MFC CString转换为string的宽字符问题解决
CString,如果项目用的是unicode的话那么实际上是CStringW类型,这个时候向string转换的时候,编译器会报错,const char* 无法转换为const w_char *,这个时候只能这个做了。
注意要加上宏
1 //tmp1极为CStringW宽字符变量
2 //转换为psText的char* 变量了
3
4 #ifdef UNICODE
5 DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, tmp1.GetBuffer(0), -1, NULL, 0, NULL, FALSE);
6 char *psText;
7 psText = new char[dwNum];
8 if (!psText)
9 delete []psText;
10 WideCharToMultiByte(CP_OEMCP, NULL, tmp1.GetBuffer(0), -1, psText, dwNum, NULL, FALSE);
11 #endif
2 //转换为psText的char* 变量了
3
4 #ifdef UNICODE
5 DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, tmp1.GetBuffer(0), -1, NULL, 0, NULL, FALSE);
6 char *psText;
7 psText = new char[dwNum];
8 if (!psText)
9 delete []psText;
10 WideCharToMultiByte(CP_OEMCP, NULL, tmp1.GetBuffer(0), -1, psText, dwNum, NULL, FALSE);
11 #endif
注意要加上宏