LPWSTR转string
时间: 2023-08-07 08:55:29 浏览: 71
LPWSTR是指向宽字符字符串的指针,而string是C++中的字符串类。要将LPWSTR转换为string,可以使用WideCharToMultiByte函数。
以下是一个示例代码:
#include <iostream>
#include <string>
#include <Windows.h>
std::string LPWSTRToString(LPWSTR lpwstr)
{
int size = WideCharToMultiByte(CP_UTF8, 0, lpwstr, -1, NULL, 0, NULL, NULL);
std::string str(size, 0);
WideCharToMultiByte(CP_UTF8, 0, lpwstr, -1, &str[0], size, NULL, NULL);
return str;
}
int main()
{
LPWSTR lpwstr = L"Hello, world!";
std::string str = LPWSTRToString(lpwstr);
std::cout << str << std::endl;
return 0;
}
在上面的代码中,LPWSTRToString函数接受一个LPWSTR参数,使用WideCharToMultiByte将宽字符转换为多字节字符串,并返回一个string对象。
注意:在使用WideCharToMultiByte函数之前,需要确保项目的字符集设置为多字节字符集(项目属性->常规->字符集)。
希望这可以帮助到你!如果还有其他问题,请随时提问。
相关推荐
















