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

使用_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;

}

 

 

 

 

你可能感兴趣的:(unicode)