简体   繁体   中英

Pass parameters as BSTR or _bstr_t?

My COM-implementing classes take parameters as BSTR (and VARIANT* ) and these get passed around internally. Generally we convert them to std::string or std::wstring but some times they are getting pased around and will be sent back into another COM call.

In these cases is it better to pass raw COM types around, or wrap them in helper-classes like _bstr_t and _variant_t ?

If you do nothing special with a BSTR but just pass it to another method, then you don't have to wrap it. You can see the BSTR as an opaque pointer.

_bstr_t (or CComBSTR which is another wrapper provider with Visual Studio) is useful when you need to allocate BSTR and don't want to manage memory yourself (and make sure you're not leaking memory), but they are not mandatory.

PS: unless I need to output a BSTR to a program that doesn't understand Unicode, I would never use an intermediary std:string to pass BSTRs around, as I would take the risk of losing information. std:wstring is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM