How to convert System::String to CString??

M

Morten Overgaard

Hi

How do I convert a managed System::String to a CString. I need to handle
unicode characters ( Japan and Chinese ) as well as "normal" ANSI
characters.

I have tried the following

char* p =
(char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(
msg ).ToPointer();
CString str(p);

which works fine as long we are not dealing with Japanese. How do I handle
both situations??

Regards Morten
 
G

Gary Chang [MSFT]

Hi Morten,

Thanks posting in the group!

You can refer to the following KB article, maybe you can find good
resolution from it:
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?id=311259

Hope that helps!


Best regards,
Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
T

Tomas Restrepo \(MVP\)

Hi Morten,
How do I convert a managed System::String to a CString. I need to handle
unicode characters ( Japan and Chinese ) as well as "normal" ANSI
characters.

I have tried the following

char* p =
(char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(
msg ).ToPointer();
CString str(p);

which works fine as long we are not dealing with Japanese. How do I handle
both situations??

You'd probably need to stay in unicode (compiling your code for unicode,
instead of ANSI). Ohh, BTW, there's a CString constructor that already takes
a System::String* as an argument :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top