The Active Template Library (ATL) is a set of template-based C++ classes that enable you to create small, fast Component Object Model (COM) objects with minimal boilerplate. Where MFC provides a full application framework, ATL focuses specifically on COM — providing implementations ofDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt
Use this file to discover all available pages before exploring further.
IUnknown, IDispatch, class factories, apartment threading models, connection points, and ActiveX control support, all with near-zero runtime overhead. ATL relies heavily on C++ templates and compile-time polymorphism, so the code generated is lean and does not require the MFC runtime.
ATL continues to be supported in Visual Studio, but like MFC, it is no longer receiving new features or documentation updates. It remains the standard way to write lightweight COM servers, shell extensions, and ActiveX controls in C++.
Why ATL for COM?
Writing COM objects manually requires implementingQueryInterface, AddRef, and Release on every object, building class factories, registering the object in the Windows registry, and handling threading correctly. ATL automates all of this through template classes while keeping the generated binary small.
CComObject
Template that adds reference counting (
AddRef/Release) and QueryInterface routing to any ATL class. It is the standard way to create heap-allocated COM objects.CComPtr / CComQIPtr
RAII smart pointers for COM interface pointers.
CComPtr<IFoo> automatically calls AddRef on assignment and Release on destruction. CComQIPtr additionally calls QueryInterface.CComBSTR
RAII wrapper around the
BSTR string type used in COM Automation. Automatically frees the string on destruction and provides conversion to/from LPCWSTR.CComVariant
RAII wrapper around
VARIANT, the polymorphic value type used in COM Automation. Supports automatic type coercion and proper VariantClear on destruction.ATL Smart Pointers vs std::shared_ptr
ATL’s CComPtr and C++‘s std::shared_ptr both manage object lifetimes but serve different purposes:
| Feature | CComPtr<T> | std::shared_ptr<T> |
|---|---|---|
| Protocol | COM AddRef/Release | Custom deleters (default: delete) |
| Type system | COM interfaces (IUnknown) | Any C++ type |
| Thread safety | Interlocked ref count (per COM threading model) | Atomic ref count |
| QueryInterface | Via CComQIPtr | Not applicable |
| Suitable for | COM objects, Windows Shell, OLE | General C++ objects |
Key ATL Classes
CComObject — Instantiable COM Objects
CComObject — Instantiable COM Objects
CComObject<CYourClass> wraps your implementation class to provide stack or heap COM objects. For heap objects use CComObject<T>::CreateInstance().CComBSTR — Safe BSTR Handling
CComBSTR — Safe BSTR Handling
CComBSTR eliminates the need to manually call SysAllocString and SysFreeString.CComVariant — Automation VARIANT Wrapper
CComVariant — Automation VARIANT Wrapper
CComVariant wraps VARIANT with constructors for common types and auto-calls VariantClear on destruction.Simple ATL COM Server Example
The following shows the minimal structure of an in-process COM server (DLL) built with ATL. In practice, you would generate this scaffolding using the ATL Project and ATL Simple Object wizards in Visual Studio.Create the ATL Module
Every ATL server has exactly one
CAtlDllModuleT or CAtlExeModuleT instance. For a DLL server:Threading Models
ATL supports all COM apartment threading models through template parameters:| Class | Threading Model | Use Case |
|---|---|---|
CComSingleThreadModel | Single-threaded apartment (STA) | UI objects, legacy COM |
CComMultiThreadModel | Multi-threaded apartment (MTA) | Background services, thread pools |
CComMultiThreadModelNoCS | MTA without critical sections | Performance-critical inner objects |