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

C++Winapi

C++ Problem Overview


I'm looking for a method, or a code snippet for converting std::string to LPCWSTR

C++ Solutions


Solution 1 - C++

The solution is actually a lot easier than any of the other suggestions:

std::wstring stemp = std::wstring(s.begin(), s.end());
LPCWSTR sw = stemp.c_str();

Best of all, it's platform independent.

Solution 2 - C++

If you are in an ATL/MFC environment, You can use the ATL conversion macro:

#include <atlbase.h>
#include <atlconv.h>

. . .

string myStr("My string");
CA2W unicodeStr(myStr);

You can then use unicodeStr as an LPCWSTR. The memory for the unicode string is created on the stack and released then the destructor for unicodeStr executes.

Solution 3 - C++

I prefer using standard converters:

#include <codecvt>

std::string s = "Hi";
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide = converter.from_bytes(s);
LPCWSTR result = wide.c_str();

Please find more details in this answer: https://stackoverflow.com/a/18597384/592651


Update 12/21/2020 : My answer was commented on by @Andreas H . I thought his comment is valuable, so I updated my answer accordingly:

> 1. codecvt_utf8_utf16 is deprecated in C++17. > 2. Also the code implies that source encoding is UTF-8 which it usually isn't. > 3. In C++20 there is a separate type std::u8string for UTF-8 because of that.

But it worked for me because I am still using an old version of C++ and it happened that my source encoding was UTF-8 .

Solution 4 - C++

  • After knowing that, the C++11-standard has rules for .c_str() method (maybe .data() too), which allows us to use const_cast, >(I mean, normally using const_cast may be dangerous)
  • we can safely take "const" input parameter.
  • Then finally, use "const_cast" instead of any unnecessary allocation and deletion.
std::wstring s2ws(const std::string &s, bool isUtf8 = true)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(isUtf8 ? CP_UTF8 : CP_ACP, 0, s.c_str(), slength, 0, 0);
    std::wstring buf;
    buf.resize(len);
    MultiByteToWideChar(isUtf8 ? CP_UTF8 : CP_ACP, 0, s.c_str(), slength,
           const_cast<wchar_t *>(buf.c_str()), len);
    return buf;
}

std::wstring wrapper = s2ws(u8"My UTF8 string!");
LPCWSTR result = wrapper.c_str();

>Note that we should use CP_UTF8 for C++'s string-literal, but in some cases you may need to instead use CP_ACP (by setting second parameter to false).

Solution 5 - C++

If you are using QT then you can convert to QString and then myqstring.toStdWString() will do the trick.

Solution 6 - C++

It's so easy, no need to apply any custom method. Try with this:

string s = "So Easy Bro"
LPCWSTR wide_string;

wide_string = CA2T(s.c_str());

I think, it will works.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionToran BillupsView Question on Stackoverflow
Solution 1 - C++Benny HilfigerView Answer on Stackoverflow
Solution 2 - C++17 of 26View Answer on Stackoverflow
Solution 3 - C++AshiView Answer on Stackoverflow
Solution 4 - C++Top-MasterView Answer on Stackoverflow
Solution 5 - C++RyanView Answer on Stackoverflow
Solution 6 - C++Sabbir PulakView Answer on Stackoverflow