How can I convert a String to a WCHAR array? Here is basically the code I'm
trying to execute (in C++) but the casting from Char* -> WCHAR* does not work:
//////////////////////////////////////////////////////
#include <windows.h>
#using <mscorlib.dll>
using namespace System;
int main(int argc, char *argv[]) {
String *s = S"this is a string";
Char cArr[] = s->ToCharArray(0,s->Length);
WCHAR *wcArr = (WCHAR*)&cArr;
return 0;
}
//////////////////////////////////////////////////////
Willy Denoyette [MVP] - 15 Sep 2004 10:31 GMT
- No need to copy a string to an array.
- You can't safely take an address of a managed object (String) and cast it
to an unmanaged pointer.
Here is what you should do.
#include <vcclr.h>
...
String *s = S"this is a string";
wchar_t __pin* wcArr = PtrToStringChars(s); // Pin the internal string
buffer and return the address
...
Willy.
PS. Please post C++ questions to: microsoft.public.dotnet.languages.vc
> How can I convert a String to a WCHAR array? Here is basically the code
> I'm
[quoted text clipped - 15 lines]
> }
> //////////////////////////////////////////////////////