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.
Asked
Active
Viewed 5.2k times
23
-
4`std::wstring someParam = std::wstring(argv[0]);` – Chris O Sep 17 '12 at 14:00
4 Answers
32
std::string MyString = CW2A (L"LPWSTR STRING");
You need to include atlstr.h
for CW2A

Raxvan
- 6,257
- 2
- 25
- 46

Chris Dargis
- 5,891
- 4
- 39
- 63
-
The `CW2A` macro converts a wide character string to an ASCII string, so why are you sticking the result back into a `wstring`? – Praetorian Sep 17 '12 at 14:04
-
-
1
-
9
Try to use following API functions :
And comparision of both methods WideCharToMultiByte() vs. wcstombs()
1
Let's say yout LPWSTR variable is myVarL:
wstring ws( myVarL );
string myVarS = string( ws.begin(), ws.end() );
should make what you want

Lambert Duran
- 42
- 4
-
3This will not properly deal with most of the possible characters in a wide string – M.M Nov 22 '18 at 06:04
0
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

Haumikj Haumer
- 19
- 1
- 5
-
1Please explain how this answer is any better than the one by Lambert Duran several years ago. And also explain how this "conversion" deals with characters outside the basic ASCII set. – TheUndeadFish Jul 30 '22 at 17:37
-