查阅了许多方法,总是失败
最常见的方法如下:
//Cstring->string 方法一:无报错但运行失败
CString cstr;
USES_CONVERSION;
string str(W2A(cstr));
//Cstring->string 方法二:
std::string strStl;
strStl=strMfc.GetBuffer(0);
报错
表示方法返回值为LPWSTR类型,而我们用了string类型进行接收
然后,我寻找从LPWSTR转string的方法,没又找到现成的方法,只能从博主“智勇双全的智勇”的文章取代码如下:《LPCWSTR或LPWSTR与string相互转换》
添加代码如下:
string WCharToMByte(LPCWSTR lpcwszStr)
{
string str;
DWORD dwMinSize = 0;
LPSTR lpszStr = NULL;
dwMinSize = WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, NULL, 0, NULL, FALSE);
if (0 == dwMinSize)
{
return FALSE;
}
lpszStr = new char[dwMinSize];
WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, lpszStr, dwMinSize, NULL, FALSE);
str = lpszStr;
delete[] lpszStr;
return str;
}
记得声明
string WCharToMByte(LPCWSTR lpcwszStr);
使用如下
std::string strStl= WCharToMByte(filePath.GetBuffer(0));
一运行发现两个都好用
之前传参出现了问题
气死我了