您的位置:首页 > 其它

_bstr_t可接受多字节、UNICODE字符串,方便用以字符集转换

2013-12-18 10:46 543 查看
使用_bstr_t需要包含的头文件:

#include <comutil.h>
#include <comdef.h>


// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <comutil.h>
#include <comdef.h>
#include <string>
using namespace std;

string ws2ms(const wstring& ws)
{
_bstr_t t = ws.c_str();
char* pchar = (char*)t;
return pchar;
}

wstring ms2ws(const string& s)
{
_bstr_t t = s.c_str();
wchar_t* pwchar = (wchar_t*)t;
return pwchar;
}

int _tmain(int argc, _TCHAR* argv[])
{
string s = "123";
wstring ws = L"456";

::MessageBoxA(0, s.c_str(), (char *)ws2ms(ws).c_str(), 0);
::MessageBoxW(0, ms2ws(s).c_str(), ws.c_str(), 0);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: