_bstr_t 和CComBSTR
2008-08-27 20:51
127 查看
_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:
它需要头文件#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,这讲出现泄漏。 |
相关文章推荐
- BSTR、LPSTR、LPWSTR、CString、VARIANT、COleVariant 、_variant_t、CComBSTR、_bstr_t
- COM中的OLECHAR ,BSTR和CComBSTR 的理解和应用
- 关于 BSTR, CComBSTR and _bstr_t
- BSTR、LPSTR、LPWSTR、CString、VARIANT、COleVariant 、_variant_t、CComBSTR、_bstr_t
- char * bstr CComBSTR 转换
- ATL基础BSTR CComBSTR SysAllocString
- BSTR、LPSTR、LPWSTR、CString、VARIANT、COleVariant 、_variant_t、CComBSTR、_bstr_t
- ATL基础BSTR CComBSTR SysAllocString
- BSTR、LPSTR、LPWSTR、CString、VARIANT、COleVariant 、_variant_t、CComBSTR、_bstr_t
- 理解COM字符串数据类型BSTR 、CComBSTR类
- BSTR、_bstr_t与CComBSTR
- BSTR、LPSTR、LPWSTR、CString、VARIANT、COleVariant 、_variant_t、CComBSTR、_bstr_t
- char, BSTR, _bstr_t, CComBSTR之间的互相转换
- char, BSTR, _bstr_t, CComBSTR之间的互相转换
- VC学习四:关于 BSTR, CComBSTR and _bstr_t
- VC++中遇到的各种数据类型BSTR、LPSTR、LPWSTR、CString、VARIANT、COleVariant 、_variant_t、CComBSTR
- wchar_t,char,string,CString,BSTR,CComBSTR,_bstr_t,VARIANT 、_variant_t 与 COleVariant相互转换
- VC中BSTR、Char*、CString和CComBSTR类型的转换
- BSTR、_bstr_t与CComBSTR相互转换
- BSTR、LPSTR、LPWSTR、CString、VARIANT、COleVariant 、_variant_t、CComBSTR、_bstr_t