Convert System::String to char*

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
 
N

Nishant S

Here's one way :-

String* s1 = S"............";
const __wchar_t __pin* pstr = PtrToStringChars(s1);
 
S

Sin

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.
 

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