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
Ben Schwehn - 24 Sep 2004 19:42 GMT
> 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
[quoted text clipped - 4 lines]
>
> Jon
you can use Marshal::StringToHGlobalUni and the corresponding ansi
function.
Example:
wchar_t* chars =
reinterpret_cast<wchar_t*>((Marshal::StringToHGlobalUni(string)).ToPointer());
hth

Signature
Ben
http://bschwehn.de
Ben Schwehn - 24 Sep 2004 19:45 GMT
> you can use Marshal::StringToHGlobalUni and the corresponding ansi
> function.
[quoted text clipped - 3 lines]
> wchar_t* chars =
> reinterpret_cast<wchar_t*>((Marshal::StringToHGlobalUni(string)).ToPointer());
also, don't forget to delete the allocated memory by calling FreeHGlobal
afterwards.
Lokkju - 24 Sep 2004 20:32 GMT
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;
}
Ioannis Vranos - 24 Sep 2004 20:32 GMT
> 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.

Signature
Ioannis Vranos
Eric Twietmeyer - 24 Sep 2004 22:00 GMT
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
> 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
[quoted text clipped - 4 lines]
>
> Jon
Jochen Kalmbach - 24 Sep 2004 22:06 GMT
=?Utf-8?B?Sm9uYXRoYW4gUG9ydGVy?= wrote:
> 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

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jonathan Porter - 26 Sep 2004 02:03 GMT
Thanks everyone for your input, I appreciate your guidance.
Thanks again!
Jon
> =?Utf-8?B?Sm9uYXRoYW4gUG9ydGVy?= wrote:
>
[quoted text clipped - 9 lines]
> See: How To Convert from System::String* to Char* in Visual C++ .NET
> http://support.microsoft.com/?kbid=311259