| Active Template Library | |
|---|---|
| Developer | Microsoft |
| Written in | C++ |
| Operating system | Microsoft Windows |
| Type | Library or framework |
| License | Proprietary |
The Active Template Library (ATL) is a set of template-based C++ classes developed by Microsoft, intended to simplify the programming of Component Object Model (COM) objects. The COM support in Microsoft Visual C++ (MSVC) allows developers to create a variety of COM objects, OLE Automation servers, and ActiveX controls.
ATL is integrated with software wizards supplied with MSVC that facilitate automatic copy-and-paste programming for basic tasks, such as configuring the library's build options and setting up the primary structure of an ATL object definition. On the COM client side ATL provides smart pointers that deal with COM reference counting. The library makes heavy use of the curiously recurring template pattern and multiple inheritance.
COM objects can also be created with Microsoft Foundation Classes (MFC), but this leads to larger binaries that require support DLLs. ATL, on the other hand, is a more lightweight alternative in situations where the graphical user interface parts of MFC are not required.
Older versions of ATL (ATL prior to version 7.1, pre-Visual Studio 2005) are incompatible with DEP because it places executable thunks in data heap. This problem can be mitigated with DEP-ATL thunk emulation with performance penalty.
In ATL version 7 (Visual Studio 2003), which directly succeeded version 3 (Visual Studio 6.0), a number of MFC classes like CString were made available in ATL, or more precisely moved to an ATLMFC common layer which is shared by both libraries. ATL version 7 also introduced attributes in C++ in an attempt to provide something similar to CLI attributes, however these have not been particularly successful, and have been deemphasized in ATL version 8 (Visual Studio 2005); the various wizards no longer generate them by default. Version 7 also introduced new string conversion classes.
On July 28, 2009, Microsoft released a patch to ATL to fix a bug that could allow ActiveX controls created using ATL to be vulnerable to a security flaw permitting remote code execution.
Since Visual Studio 2013 (ATL version 12), all ATL code is statically linked, eliminating the DLL.
ATL version is defined by ATL_VER macros and can be queried via AtlGetVersion() function.
ATL includes several wrapper classes that encapsulate COM types in order to simplify programs' usage thereof. These classes provide a façade over the management of runtime COM data structures, along with RAII semantics. The most commonly used classes include:
CComPtr<T> general-purpose smart-pointer,CComBSTR BSTR wrapper,CComVariant VARIANT wrapper, andCComSafeArray<T> SAFEARRAY wrapper.The CComPtr class template is a generic smart pointer class that is parameterized on the type of COM interface being wrapped. Instances of this class are initialized using an appropriate COM interface pointer, passed as an argument either to the class constructor or the object's overloaded assignment operator.
Instances of the CComPtr class always perform reference counting, and there is no ATL analogue to the single-owner unique_ptr smart pointer class as found in the C++11 standard library. Instead, CComPtr can be used for both single-owner and multiple-owner scenarios.
The CComSafeArray<T> class template is a wrapper class for the COM SAFEARRAY type, designed to ease handling of safe arrays. The class is generic, being parameterized on the element type of the safe array, although only a subset of C++ types are supported as such.
Because it is a type that encapsulates a growable array, the functionality provided by CComSafeArray<T> overlaps with that of the vector sequence container in the standard template library. While STL vectors are more efficient at dynamic resizing, they do not provide the ABI stability ensured by safe arrays as a COM type, which allow the latter to be transferred across module and programming language boundaries.
Although not formally part of ATL, MSVC also includes additional C++ RAII classes to simplify management of COM types. These compiler COM support classes can be used as replacement for or in combination with ATL, and includes:
_com_ptr_t smart-pointer that decorates the COM interface name with a "Ptr" suffix,_bstr_t BSTR wrapper,_variant_t VARIANT wrapper, and_com_error HRESULT wrapper.Note that as of Visual Studio 2012, the compiler COM support classes does not include a SAFEARRAY wrapper.
{{Cite Q}}: Missing first/last names in Wikidata for Mark Davis (help)