3

I have the following code to convert a String to a CFString and backwarts:

string path = @"C:\Users\ayi\Desktop\Failed\AngryBirds.ipa";

IntPtr p_path = __CFStringMakeConstantString(path);

CoreFoundation.CFString cfstring = new CoreFoundation.CFString(p_path);

string result = cfstring.ToString();

The problem is path != result where the path contain some Chinese characters like @"C:\Users\ayi\Desktop\中文\AngryBirds.ipa";

How can I cast string that contain Chinese to cfstring in C#?

Cœur
  • 37,241
  • 25
  • 195
  • 267
pizixie
  • 31
  • 2

1 Answers1

0

You can convert a c-like string to CFString with the CFStringCreateWithCString function:

CFStringCreateWithCString(kCFAllocatorDefault, myCString, kCFStringEncodingUTF8);

Then you need to figure-out how to convert C# String to c-like string. I am unfortunately not a C# developer, so here is a link which seems ok:

C++ .NET convert System::String to std::string

Note: use std::string::c_str() to convert C++ string to c-like string myCppString.c_str(). This operation is mostly accessing inner memory, so very fast.

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85