-2

i tried google search a lot but only found the ways how to convert string to const LPCWSTR

I try this code but i can't success

string s = "ABCD";
wstring stemp = wstring(s.begin(), s.end());
WCHAR* sw = stemp.c_str();
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

1 Answers1

0

Use MultiByteToWideChar:

#include <string>
#include <Windows.h>

int main() {
    // Set /utf-8 or use u8 strings
    const std::string str = "\u7EDD\u4E0D\u4F1A\u653E\u5F03\u4F60";
    const int size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0);

    std::wstring wstr(size, '\0');
    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstr[0], (int)wstr.size());
    
    WCHAR* sw = &wstr[0];
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93