ATL CComPtr

COM smart pointers take care of reference counting, add type safety, and shorten the source code a lot.

There are 2 smart pointer classes, CComPtr and CComQIPtr. CComQIPtr is superset of CComPtr, but is not a child class.

COM smart pointers are not to be confused with C++ smart pointers.

_com_ptr_t is a VC++ compiler COM support extension. It’s compiler native, so there’s no dependency on atl.dll.

// Reference counting handled for free
void Load()
{
    CComPtr<IMyClass> x;
    x.CoCreateInstance(OLESTR("MyLib.MyClass"));
    CComQIPtr<IMyClass2> y(x);
    y->DoSomething();
}

References

CComPtr Class | Microsoft Learn CComQIPtr Class | Microsoft Learn _com_ptr_t Class | Microsoft Learn