CComVariant vs. _variant_t, CComBSTR vs. _bstr_t

Rob picture Rob · Feb 18, 2010 · Viewed 8.2k times · Source

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?

Answer

JBRWilkinson picture JBRWilkinson · Feb 18, 2010

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