com

_bstr_t在VC中是为了兼容BSTR类型而增加的,也就是为了实现LPCSTR与BSTR转换。

它需要头文件#include <comdef.h>

_bstr_t 是BSTR的包装类
转换方法

LPSTR strDemo="Test";
_bstr_t bstr(strDemo);
建议加上try,catch,用于catch(_com_error &e)



The following pseudocode shows the typical use of CComBSTR:

HRESULT CMyObject::MyMethod(IOtherObject* pSomething)
{
CComBSTR bstrText(L"Hello");
bstrText += " again"; // LPCSTR conversion
bstrText.ToUpper();
pSomething->Display(bstrText); // [in] parameter
MessageBoxW(0, bstrText, L"Test", MB_OK); // Assumes Windows NT
}
As you can see, CComBSTR significantly simplifies the use of BSTRs. Four uses of CComBSTR, however, require special care:
Freeing the BSTR explicitly


Using CComBSTR as an [out] parameter


Using a CComBSTR automatic variable in right-side assignments


Using a CComBSTR member variable in right-side assignments
当 在BSTR*所在的位置作为一个[out]参数传递CComBSTR时,你必须先调用Empty释放string的内容,就象下面这样:HRESULT CMyObject::MyMethod2(ISomething* p, /*[out]*/ BSTR* pbstr)
{
CComBSTR bstrText;

bstrText = L"Some assignment"; // BSTR is allocated.

bstrText.Empty(); // Must call empty before
pSomething->GetText(&bstrText); // using as an [out] parameter.
if(bstrText != L"Schaller")
bstrText += "Hello"; // Convert from LPCSTR.
}
因为在重写BSTR内容之前方法COM 为[out] 参数的规则是并不调用sysfreestring,这讲出现泄漏。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值