Ever since the 5.0 release, Visual C++ has provided several COM helper
classes as part of its run-time library. These classes—including
_com_ptr_t, _bstr_t, and
_variant_t—provide support similar to the
CComPtr, CComBSTR, and
CComVariant classes described in this chapter. How do you decide whether
to use the run-time classes or the C++ classes? Both sets of classes perform
nearly identically, with a few exceptions:
Unlike _bstr_t, CComBSTR provides an operator overload that allows it to be used as an [out] parameter in place of a
BSTR*. _bstr_t allows you to extract an LPCTSTR, and it implements a
reference-counting technique that makes it generally more efficient
than CComBSTR.
Unlike CComPtr::CoCreateInstance, the equivalent
_com_ptr_t::CreateInstance method follows a three-step process
for returning the desired pointer:
Call ::CoCreateInstance, asking for
IUnknown.
Call ::OleRun.
Call QueryInterface for the desired interface.
This algorithm is less efficient, especially when dealing with
remote servers, but it's more friendly to Automation servers that require
a call to OleRun.
The Visual C++ run-time classes throw _com_error exceptions instead of returning error codes. Although this technique is
desirable when you're using the run-time classes in COM
client code, you must take special precautions when using the run-time classes
in COM server code because the COM specification expressly
forbids throwing an exception across the boundaries of a COM server.
The #import keyword, a powerful extension to the Visual C++
compiler, optionally generates code that uses the
run-time support classes. If you're using the
#import keyword, you might prefer the run-time classes over the ATL classes.
The ATL classes are, at least in theory, more portable because
they don't depend on special extensions to the C++ compiler. At the
time of this writing, however, only the Microsoft Visual C++
compiler supports ATL.
The run-time COM classes don't depend on ATL, so they might
be more appropriate than the ATL classes for use in MFC applications.
Neither set of classes is clearly preferable, so the decision is up to you.
It's likely that Microsoft will continue to support both sets of classes for some
time. In fact, you can use both the run-time classes and the ATL client classes in
a single application. For fairly obvious reasons, however, we recommend
using the run-time classes from within MFC applications and the ATL classes
within an ATL project.