leggo
leggo

Reputation: 319

Convert LPWSTR to string

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.

Upvotes: 24

Views: 56541

Answers (4)

Haumikj Haumer
Haumikj Haumer

Reputation: 31

This is how you can convert LPWSTR to string:

// Assume you have initialized the lpwstr variable
std::wstring wString;
wString.append(&lpwstr[0]);
std::string convertedString(wString.begin(), wString.end());

Easy peasy

Upvotes: 0

Lambert Duran
Lambert Duran

Reputation: 62

Let's say yout LPWSTR variable is myVarL:

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

should make what you want

Upvotes: 3

Chris Dargis
Chris Dargis

Reputation: 6053

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

You need to include atlstr.h for CW2A

Upvotes: 33

rkosegi
rkosegi

Reputation: 14678

Try to use following API functions :

  1. WideCharToMultiByte

  2. wcstombs

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

Upvotes: 9

Related Questions