Convert std::string to LPCWSTR (best way in c++)

Hi!

How can I convert an std::string to a LPCWSTR What is the best way to do it in C++

Thanks.

"Ciao guaglio!"




Answer this question

Convert std::string to LPCWSTR (best way in c++)

  • FlyFisher

    Andrew Revvo wrote:
    I recommend make new software, using std:wstring only, because all new Microsoft systems are UNICODE internally

    The Windows API is TCHAR, and regardless of whether it uses unicode or ansi internally, the API is the only thing you access. Therefore, if you use the Windows API, you need to access it with TCHAR.

    Andrew Revvo wrote:
    and there is only a small count of working Windows9x systems.

    For the remaining Windows9x systems, you always have the Microsoft Layer for Unicode, which allows your unicode app to run on these systems.

    Andrew Revvo wrote:
    Using of TCHAR is a poor design in Windows, unfortunately, because we should create two different exe files for unicode and ansi environment.

    Although TCHAR's primary use is cited for developing an ansi and unicode EXE, its use extends to more than just that. You can use it to create libraries that work with both unicode and ansi projects with minimal pain. Whereas with ANSI apps, you have to rewrite the entire project to convert it to unicode, with TCHAR apps, you just need to define UNICODE.

    Having said that, DLLs should be developed Petzold-Windows style (ie. have both a W entry point and A entry point).

  • AxeldraX

    FabioDeSantis wrote:

    How can I convert an std::string to a LPCWSTR What is the best way to do it in C++

    Instead of using a std::string, use a std::wstring (also called a std::basic_string<wchar_t>).

    I get the feeling you want to pass a std::string type to a Win32 API. Those APIs don't take LPCWSTRs (or even LPCSTRs), they take a LPCTSTR (long pointer to a tchar-string). In this case, your question should have been: "How do I convert a std::string to a LPCTSTR "

    Instead of using a std::string use a std::basic_string<TCHAR>.



  • Pierce Blaylock

    Perfect! Thank you so much!

    "Ciao guaglio!"



  • Matt Warren

    I recommend make new software, using std:wstring only, because all new Microsoft systems are UNICODE internally and there is only a small count of working Windows9x systems.

    Using of TCHAR is a poor design in Windows, unfortunately, because we should create two different exe files for unicode and ansi environment.

    Using of a std::wstring is simple. There is a fastest way to convert to it from Windows API functions or use it for Windows API calling.

    For compatible conversions use this code:

    std::string ws2s(const std::wstring& s)
    {
    int len;
    int slength = (int)s.length() + 1;
    len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0);
    char* buf = new char[len];
    WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, buf, len, 0, 0);
    std::string r(buf);
    delete[] buf;
    return r;
    }

    This function and s2ws() are checked for bugs. Only one problem may be available, if you will use these functions in expressions with c_str(). You should create a local variable in some cases, because C++ may call a string destructor and destroy string object before API calling, so this API function may get a pointer to a destructed memory.



  • Goofs

    std::wstring s2ws(const std::string& s)
    {
     int len;
     int slength = (int)s.length() + 1;
     len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
     wchar_t* buf = new wchar_t[len];
     MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
     std::wstring r(buf);
     delete[] buf;
     return r;
    }

    std::string s;

    #ifdef UNICODE
    std::wstring stemp = s2ws(s); // Temporary buffer is required
    LPCWSTR result = stemp.c_str();
    #else
    LPCWSTR result = s.c_str();
    #endif



  • John-at-WSRB

    Although you can use this function to convert a ANSI string to wide characters (or if you're comfortable with C++ iostreams:

  • Convert std::string to LPCWSTR (best way in c++)