Groups | Blog | Home
all groups > dotnet sdk > september 2004 >

dotnet sdk : How to convert String to WCHAR ?



Dane
9/13/2004 3:45:04 PM
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]
9/15/2004 11:31:28 AM
- 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

[quoted text, click to view]

AddThis Social Bookmark Button