Dynamically creating "LPCWSTR" variable from std::string variable

178 views Asked by At

Currently I have the code (typedef _Null_terminated_ CONST WCHAR *LPCWSTR;):

LPCWSTR wchar_string(L"ABC");

But need to enable something like this:

std::string s("ABC");
...
LPCWSTR wchar_string(/* create somehow from variable s */);
1

There are 1 answers

0
JDługosz On

std::string is an 8-bit character string. wchar_string is an array of Wide (UTF-16) code points. That requires by definition that a transcoding operation take place, and it's not just a simple assignment.

First, how is the 8-bit string encoded? Is it UTF-8, Windows ANSI "current code page", Latin-1 only, or what?

Second, call a function like Windows' MultiByteToWideChar to do the heavy lifting, and be sure to manage memory for the result buffer that wchar_string is pointing to.