I am using ATL (VS2008, so ATL9 IIRC) to create COM objects and have been using the CComVariant
class (defined in atlcomcli.h
) to manage VARIANT
types. However, there is also another VARIANT
wrapper called _variant_t
. Is there any difference between CComVariant
and _variant_t
and which one should I be using?
Similarly, there are two BSTR
wrappers available - CComBSTR
and _bstr_t
. Again, which should I prefer and why?
_variant_t and _bstr_t are provided by the compiler as COM support classes and get used when you use constructs like #import . You can use them if you like.
CComVariant and CComBSTR are provided by the ATL libraries.
Whether you use the COM Support classes or the ATL classes is up to you. If you often need to do operations like attaching to 'raw' BSTRs or VARIANTs, the COM Support classes may be a safer bet.
There are some behavioural differences (check the docs), the most important of which seems to be that the COM Support classes will throw a _com_error& exception when something fails. If you don't want to do exception-handling, go with the ATL classes.
One major difference is that ATL's classes do not throw exceptions, and the compiler support classes do (_com_exception
, specifically).
_bstr_t
is reference-counted while CComBSTR
is more of a raw wrapper.
I use both depending on the task at hand. As stated before _variant_t and _bstr_t are more basic whereas the ATL classes are more high level (the nicer counterpart to MFC). My advise is to look a bit at the definitions of the classes. All of them are only helpers for smaller, better readable code but still contain certain pitfalls in regards to management of memory and object references. So you have to know a little bit about their internals and the documentation is often not very clear about that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With