Convert System::String to char*

  • Thread starter Thread starter Tim Mulholland
  • Start date Start date
T

Tim Mulholland

How can i (using Managed C++) convert a System::String object containing
basic text into a char* object?

Thanks in advance,

Tim
 
Here's one way :-

String* s1 = S"............";
const __wchar_t __pin* pstr = PtrToStringChars(s1);
 
Here's one way :-
String* s1 = S"............";
const __wchar_t __pin* pstr = PtrToStringChars(s1);

I think he meant a char*, as in single byte char*... Here's one way :

String foo= "Hello";
IntPtr ptr = Marshal::StringToCoTaskMemAnsi(foo);
char *singleByteFoo= (char*)ptr.ToPointer();
// do stuff with singleByteFoo
Marshal::FreeCoTaskMem(ptr);
// here, singleByteFoo is not valid anymore

Alex.
 
Back
Top