Convert LPWSTR to string

44,508

Solution 1

Try to use following API functions :

  1. WideCharToMultiByte

  2. wcstombs

And comparision of both methods WideCharToMultiByte() vs. wcstombs()

Solution 2

std::string MyString = CW2A (L"LPWSTR STRING");

You need to include atlstr.h for CW2A

Solution 3

Let's say yout LPWSTR variable is myVarL:

wstring ws( myVarL ); 
string myVarS = string( ws.begin(), ws.end() );

should make what you want

Share:
44,508

Related videos on Youtube

leggo
Author by

leggo

Updated on November 22, 2020

Comments

  • leggo
    leggo over 3 years

    Function CommandLineToArgvW is giving me commandline arguments in LPWSTR type. I need these arguments in string. Would someone please tell me how to convert LPWSTR to string?
    I'm using mingw.

    • Chris O
      Chris O almost 12 years
      std::wstring someParam = std::wstring(argv[0]);
  • Praetorian
    Praetorian almost 12 years
    The CW2A macro converts a wide character string to an ASCII string, so why are you sticking the result back into a wstring?
  • Erik Aronesty
    Erik Aronesty over 11 years
    What do you #include for CW2A?
  • Louis Waweru
    Louis Waweru about 10 years
    @ErikAronesty atlstr.h
  • M.M
    M.M over 5 years
    This will not properly deal with most of the possible characters in a wide string
  • sergiol
    sergiol over 3 years
    This will not go well for japanese characters.