Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CComVariant vs. _variant_t, CComBSTR vs. _bstr_t

Tags:

c++

com

atl

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?

like image 601
Rob Avatar asked Feb 18 '10 13:02

Rob


3 Answers

_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.

like image 182
JBRWilkinson Avatar answered Nov 11 '22 14:11

JBRWilkinson


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.

like image 30
Kim Gräsman Avatar answered Nov 11 '22 13:11

Kim Gräsman


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.

like image 1
rotti2 Avatar answered Nov 11 '22 14:11

rotti2