code_junkie

How to convert std::string to LPCWSTR in C++ (Unicode)

Nov 10th, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. std::wstring s2ws(const std::string& s)
  2. {
  3. int len;
  4. int slength = (int)s.length() + 1;
  5. len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
  6. wchar_t* buf = new wchar_t[len];
  7. MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
  8. std::wstring r(buf);
  9. delete[] buf;
  10. return r;
  11. }
  12.  
  13. std::wstring stemp = s2ws(myString);
  14. LPCWSTR result = stemp.c_str();
  15.  
  16. #include <atlbase.h>
  17. #include <atlconv.h>
  18.  
  19. . . .
  20.  
  21. string myStr("My string");
  22. CA2W unicodeStr(myStr);
Add Comment
Please, Sign In to add comment