How do I convert the contents of a Platform::String to be used by functions that expect a char* based string? I'm assuming WinRT provides helper functions for this but I just can't find them.
Thanks!
How do I convert the contents of a Platform::String to be used by functions that expect a char* based string? I'm assuming WinRT provides helper functions for this but I just can't find them.
Thanks!
Here is a very simple way to do this in code w/o having to worry about buffer lengths. Only use this solution if you are certain you are dealing with ASCII:
Keep in mind that in this example, the
char*
is on the stack and will go away once it leaves scopeA solution using wcstombs:
Platform::String::Data()
will return awchar_t const*
pointing to the contents of the string (similar tostd::wstring::c_str()
).Platform::String
represents an immutable string, so there's no accessor to get awchar_t*
. You'll need to copy its contents, e.g. into astd::wstring
, to make changes.There's no direct way to get a
char*
or achar const*
becausePlatform::String
uses wide characters (all Metro style apps are Unicode apps). You can convert to multibyte usingWideCharToMultiByte
.You shouldn't cast a wide character to a char, you will mangle languages using more than one byte per character, e.g. Chinese. Here is the correct method.
There's the
String::Data
method returningconst char16*
, which is the raw unicode string.Conversion from unicode to ascii or whatever, i.e.
char16*
tochar*
, is a different matter. You probably don't need it since most methods have theirwchar
versions these days.