tags:

views:

600

answers:

4

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?

+5  A: 

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

JBRWilkinson
A: 

BSTR and VARIANT are data types that are wrapped by the CComBSTR/_bstr_t and the CComVariant/_variant_t classes.
So as stated before the classes provide more methods etc.

Simon Linder
+3  A: 

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.

Kim Gräsman
+1 I'd say that this is the key difference. With ATL you have to write a lot of extra error-checking code. On the other side if you don't want to bother with exceptions you should prefer ATL classes.
sharptooth
+1  A: 

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.

rotti2
+1 This is really the case when spending an hour reading the source gives you much better understanding.
sharptooth