C++ 将 LPWSTR 转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12460712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:17:26  来源:igfitidea点击:

Convert LPWSTR to string

c++winapimingw

提问by leggo

Function CommandLineToArgvWis giving me commandline arguments in LPWSTRtype. I need these arguments in string. Would someone please tell me how to convert LPWSTRto string?
I'm using mingw.

函数CommandLineToArgvW给了我命令行参数的LPWSTR类型。我需要在string. 有人能告诉我如何转换LPWSTRstring吗?
我正在使用 mingw。

采纳答案by rkosegi

Try to use following API functions :

尝试使用以下 API 函数:

  1. WideCharToMultiByte

  2. wcstombs

  1. 宽字符到多字节

  2. wcstombs

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

以及两种方法的比较WideCharToMultiByte() 与 wcstombs()

回答by Chris Dargis

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

You need to include atlstr.hfor CW2A

您需要包括atlstr.h用于CW2A

回答by Lambert Duran

Let's say yout LPWSTR variable is myVarL:

假设您的 LPWSTR 变量是 myVarL:

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

should make what you want

应该做你想要的