System::String to C-Style String

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All - Hoping someone might be able to point me in the right direction.
I have a System::String that is part of a managed class. Within a member
function, I need to convert this String to a C-Style null terminated
character array so that it can be passed to a legacy C function. Any ideas?
I've tried putting it to a STL string first, but no luck.
Thanks!

Jon
 
Jonathan said:
Hello All - Hoping someone might be able to point me in the right direction.
I have a System::String that is part of a managed class. Within a member
function, I need to convert this String to a C-Style null terminated
character array so that it can be passed to a legacy C function. Any ideas?
I've tried putting it to a STL string first, but no luck.
Thanks!

Jon
you can use Marshal::StringToHGlobalUni and the corresponding ansi
function.

Example:

wchar_t* chars =
reinterpret_cast<wchar_t*>((Marshal::StringToHGlobalUni(string)).ToPointer());


hth
 
you can use Marshal::StringToHGlobalUni and the corresponding ansi
function.

Example:

wchar_t* chars =
reinterpret_cast<wchar_t*>((Marshal::StringToHGlobalUni(string)).ToPointer());


also, don't forget to delete the allocated memory by calling FreeHGlobal
afterwards.
 
Jonathan said:
Hello All - Hoping someone might be able to point me in the right direction.
I have a System::String that is part of a managed class. Within a member
function, I need to convert this String to a C-Style null terminated
character array so that it can be passed to a legacy C function. Any ideas?
I've tried putting it to a STL string first, but no luck.
Thanks!


You may use either the Chars property, or one of the member functions
ToCharArray(), CopyTo().


Personally I would use ToCharArray() to copy to a wchar_t * or if you
are sure that you use only English characters, the Chars property to
copy in a char array, or std::string etc.
 
const char * to_cStr(String * str)
{
char * rStr = new char[256];
const char* c_str = (const char*)
(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str)).ToPointer();
strcpy(rStr,c_str);
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr((void*)c_str));
return rStr;
}
String * from_cStr(const char *str)
{
String *rStr;
String *c_str = new System::String(str);
rStr = c_str;
return rStr;
}
 
Hi,

If you like to do things directly, you can also use the function defined in
vcclr.h, PtrToStringChars, as that takes a System::Strying and returns a
pointer (wide char, System::Char) to the underlying characters. You can
then perform your own memory management and conversion, if necessary, to
char*. However, if you just need wchar_t* then you can use directly the
returned pointer from PtrToStringChars and there is no overhead at all (you
just have to remember to pin and then release while you are using this).

-Eric
 
=?Utf-8?B?Sm9uYXRoYW4gUG9ydGVy?= said:
Hello All - Hoping someone might be able to point me in the right
direction. I have a System::String that is part of a managed class.
Within a member function, I need to convert this String to a C-Style
null terminated character array so that it can be passed to a legacy C
function. Any ideas? I've tried putting it to a STL string first,
but no luck. Thanks!

There are at least tree or more different methods to do this.

See: How To Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Back
Top