2008-7-4 12:50:23
 
vc中char*转换成BSTR

方法一,使用SysAllocString等API函数。
例如:

BSTR bstrText = ::SysAllocString(L"Demo");
BSTR bstrText = ::SysAllocStringLen(L"Demo",4);
BSTR bstrText = ::SysAllocStringByteLen("Demo",4);

方法二,使用COleVariant或_variant_t。
例如:

_variant_t strVar("This is a test");
BSTR bstrText = strVar.bstrVal;

方法三,使用_bstr_t,这是一种最简单的方法。
例如:

BSTR bstrText = _bstr_t("This is a test");

方法四,使用CComBSTR。
例如:

BSTR bstrText = CComBSTR("This is a test");


CComBSTR bstr("This is a test");
BSTR bstrText = bstr.m_str;

方法五,使用ConvertStringToBSTR。
例如:

char* lpszText = "Test";
BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);
这儿要#include <comutil.h>
还要导入comsupp.lib文件哦。


[Blog.Const.Net.Cn]

永久链接地址:知识库 vc中char*转换成BSTR
 
类别:知识库|浏览(10)