天天看点

CString转换为string的做法

作者:朱金灿

来源:http://blog.csdn.net/clever101

     CString转换为std::string,网上通行的做法是利用CString的GetBuffer函数。具体做法如下(编译环境为VS C++ 2005+sp1, Win XP+sp3,多字节字符集编译,以下同):

CString strMFC= _T("Hello!");

std::string str2(strMFC.GetBuffer());

strMFC.ReleaseBuffer();

      这种做法在unicode字符集下也可行。今天我探索了一种新做法,具体做法是:

CString strMFC= _T("Hello!");

TCHAR *psz = (TCHAR*)(LPCTSTR)strMFC;

std::string str3 = psz;

这种做法在unicode字符集下也可行。