CString、TCHAR、WCHAR 字符串等转BSTR的几种方法。

1._bstr_t 

_bstr_t bstrText = _T("aaaa");
CString strText(_T("aaaa"));
bstrText = strText; 

其它字符串方法雷同。

优点:使用方法简单。

缺点:需要如下引用文件

Header: comutil.h

Lib: comsuppw.lib or comsuppwd.lib

 

2.CComBSTR

CComBSTR bstrText = _T("aaaa);
CString strText(_T("aaaa));
bstrText = strText;

优点:使用方便

缺点:不能直接赋值const类型的字符串(如LPCTSTR)

 

3.CString 直接转换

CString strText(_T("aaaa"));
BSTR bstrText = strText.AllocSysString();
SysFreeString(bstrText);

优点:不用借助其他类

缺点:需要自己分配以及释放内存  

  

 

原文地址:https://www.cnblogs.com/lebronjames/p/2988410.html