BSTR与std::string相互转换



1.BSTR -> std::string 举例如下

BSTR bstrTest = ::SysAllocString(L”Test”);

_bstr_t bstr_t(bstrTest);

std::strStl = bstr_t;

SysFreeString(bstrTest );

 

2.std::string -> BSTR 举例如下

std::string name = "helloworld";

 _bstr_t bstr_t(name.c_str());

BSTR ret_val = bstr_t.GetBSTR(); 


3.CString->std::string 举例如下:

CString strMfc=“test“;
std::string strStl;

#ifdef _UNICODE

USES_CONVERSION
strStl=W2A(strMfc.LockBuffer());

strMfc.UnlockBuffer();

#else

strStl = strMfc.GetBuffer(0);

strMfc.ReleaseBuffer();

#endif

 

4.std::string->CString  举例如下:

CString strMfc;

std::string strStl=“test“;

strMfc=strStl.c_str();


你可能感兴趣的:(C/C++)