读书人

关于CString和const char*的转换,该如

发布时间: 2012-05-03 14:06:56 作者: rapoo

关于CString和const char*的转换
CString fullPath(_T("xxx"))
首先是使用了
const char* filename =fullPath.GetBuffer(sizeof(fullPath));
提示错误error C2440: “初始化”: 无法从“wchar_t *”转换为“const char *”

然后尝试了
char szStr[256] = {0};
wcstombs(szStr, fullPath, fullPath.GetLength());
出现26个错误,大概是默认库冲突一类的,没有解决方法

请问怎么能把CString转换const char*,unicode,VS2010

[解决办法]
试试 _bstr_t

Header: comutil.h

Lib: comsuppw.lib or comsuppwd.lib

C/C++ code
#include <iostream>#include <string>#include "comutil.h"#pragma comment(lib, "comsuppw.lib")using namespace std;int main() {    // 窄字符输入,宽字符输出    _bstr_t strTmp = "hello";    wcout << (wchar_t*) strTmp << endl;    // 宽字符输入,窄字符输出    strTmp = L"hello";  // 或者 strTmp = (LPCTSTR)fullPath;    cout << (char*)strTmp << endl;    system("pause");    return 0;}
[解决办法]
USES_CONVERSION;
T2A 或者W2A

读书人网 >C++

热点推荐