您的位置:首页 > 编程语言 > C语言/C++

将MFC的CString 类型转化为C++标准数据类型std::string

2011-09-14 12:54 621 查看
std::string CStringToSTDStr(const CString& theCStr)
{
// Convert the CString to a regular char array
const int theCStrLen = theCStr.GetLength();
char *buffer = (char*)malloc(sizeof(char)*(theCStrLen+1));
memset((void*)buffer, 0, sizeof(buffer));
WideCharToMultiByte(CP_UTF8, 0, static_cast<cstring>(theCStr).GetBuffer(), theCStrLen, buffer, sizeof(char)*(theCStrLen+1), NULL, NULL);
// Construct a std::string with the char array, free the memory used by the char array, and
// return the std::string object.
std::string STDStr(buffer);
free((void*)buffer);
return STDStr;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string mfc c++ buffer null
相关文章推荐