Hi all,
We have a mixed mode managed c dll here and I need help.
The internals of the dll are using native c and the interface is exposing managed c so that c# can call it. We are passing a System.String into the dll and need to convert it to char*.
Currently the developer is looping for each character in the string and placing the value into the char. There has got to be a better way.
Anyone?
Thanks
FDB
-
-
http://support.microsoft.com/?kbid=311259
disclaimer: C/C++ is yucky to me
-
I have my answer. I will post it so that it is in the archives incase anyone else needs it.
System::String^ CString2DotNetOut()
{char* cString = "20 chars for testing";
System::String^ sString = System::Runtime::InteropServices::Marshal::PtrToStringAnsi(System::IntPtr(cString));
return sString;
}void DotNetString2CStringOut(System::String^ sString)
{char* cString = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(sString).ToPointer();
cout << cString;
}
FDB -
Don't forget to free the allocation made by StringToHGlobalAnsi...

Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.