djcouchycouch djcouchycouch - 29 days ago 17
C++ Question

How to convert Platform::String to char*?

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!

Answer

Platform::String::Data() will return a wchar_t const* pointing to the contents of the string (similar to std::wstring::c_str()). Platform::String represents an immutable string, so there's no accessor to get a wchar_t*. You'll need to copy its contents, e.g. into a std::wstring, to make changes.

There's no direct way to get a char* or a char const* because Platform::String uses wide characters (all Metro style apps are Unicode apps). You can convert to multibyte using WideCharToMultiByte.

Comments