close
Jump to content

Active Template Library

From Wikipedia, the free encyclopedia

Active Template Library
DeveloperMicrosoft
Written inC++
Operating systemMicrosoft Windows
TypeLibrary or framework
LicenseProprietary

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.[1][2]

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.[1]

History

[edit]

COM objects can also be created with Microsoft Foundation Classes (MFC), but this leads to larger binaries that require support DLLs.[3] ATL, on the other hand, is a more lightweight alternative in situations where the graphical user interface parts of MFC are not required.[4]

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.[5][6] This problem can be mitigated with DEP-ATL thunk emulation with performance penalty.[7][8]

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.[9]

Since Visual Studio 2013 (ATL version 12), all ATL code is statically linked, eliminating the DLL.[10][11]

ATL version is defined by ATL_VER macros and can be queried via AtlGetVersion() function.[12]

Support classes

[edit]

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.[13] The most commonly used classes include:

  • CComPtr<T>[14] general-purpose smart-pointer,
  • CComBSTR[15] BSTR wrapper,
  • CComVariant[16] VARIANT wrapper, and
  • CComSafeArray<T>[17] SAFEARRAY wrapper.[18]

CComPtr

[edit]

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.[19]

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.[20]

CComSafeArray

[edit]

The CComSafeArray<T> class template is a wrapper class for the COM SAFEARRAY type, designed to ease handling of safe arrays.[21] 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.[22]

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.[23] 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.[23]

Compiler COM support

[edit]

Although not formally part of ATL, MSVC also includes additional C++ RAII classes to simplify management of COM types.[24] These compiler COM support classes can be used as replacement for or in combination with ATL, and includes:

  • _com_ptr_t[25] smart-pointer that decorates the COM interface name with a "Ptr" suffix,
  • _bstr_t[26] BSTR wrapper,
  • _variant_t[27] VARIANT wrapper, and
  • _com_error[28] HRESULT wrapper.

Note that as of Visual Studio 2012, the compiler COM support classes does not include a SAFEARRAY wrapper.

See also

[edit]

References

[edit]

Citations

[edit]
  1. 1 2 Grimes, Richard (1998). "ATL and COM". ATL COM (1st ed.). Wrox Press. pp. 5–22. ISBN 1-86100-249-1.
  2. Grimes, Richard (1999). "Building and calling a COM object using ATL". Beginning ATL 3 COM Programming (1st ed.). Wrox Press. p. 63. ISBN 978-1-86100-120-7.
  3. Williams, Mickey (2000). "Active Documents". Visual C++ 6 Unleashed (1st ed.). Sams. p. 733. ISBN 978-0-672-31241-0.
  4. Whitney, Tyler (November 4, 2016). "Recommendations for Choosing Between ATL and MFC". Microsoft Learn. Microsoft.
  5. Howard, Michael (January 29, 2008). "New NX APIs added to Windows Vista SP1, Windows XP SP3 and Windows Server 2008". Michael Howard's Web Log. Microsoft. Archived from the original on January 30, 2008.
  6. swiat (June 12, 2009). "Understanding DEP as a mitigation technology part 1". MSRC Blog. Microsoft Security Response Center.
  7. "Applications Using Older ATL Components May Experience Conflicts with DEP". Microsoft Support. Microsoft. January 29, 2008. Archived from the original on January 23, 2009. Retrieved January 23, 2009.
  8. Bridge, Karl (February 2, 2021). "UpdateProcThreadAttribute function (Processthreadsapi.h)". Microsoft Learn. Microsoft.
  9. "ATL Security Update". MSDN. Microsoft. July 28, 2009. Retrieved September 19, 2011.
  10. "ATL and MFC changes and fixes in Visual Studio 2013". August 20, 2013. Archived from the original on August 27, 2013. Retrieved August 27, 2013.
  11. "ATL and MFC changes and fixes in Visual Studio 2013". August 20, 2013.
  12. Whitney, Tyler; Sharkey, Kent; nxtn; Blome, Mike; Hogenson, Gordon; Cai, Saisang (November 4, 2016). "ATL Text Encoding Functions". Microsoft Learn. Microsoft. Archived from the original on May 21, 2023.
  13. Tavares et al. (2007), p. 99.
  14. Microsoft MSDN: CComPtr Class
  15. Microsoft MSDN: CComBSTR Class
  16. Microsoft MSDN: CComVariant Class
  17. Microsoft MSDN: CComSafeArray Class
  18. Fertitta, Kirk (September 2005). "Navigating the landscape of IVI". AUTOTESTCON 2005. International Automatic Testing Conference. Orlando: IEEE. pp. 801–811. doi:10.1109/AUTEST.2005.1609236. ISBN 0-7803-9101-2.
  19. Grimes (1998), p. 321.
  20. Whitney, T.; Laroche, N.; Sharkey, K.; et al. (November 19, 2019). "How to: Create and use CComPtr and CComQIPtr instances". Microsoft Learn. Microsoft. Retrieved June 30, 2026.
  21. DiLascia, Paul (August 2003). "C++ Q&A: Finding a Win32 Handle, HTML in CHtmlCtrl". MSDN Magazine. Vol. 18, no. 10. Microsoft. ISSN 1528-4859.
  22. Tavares et al. (2007), ch. 3.
  23. 1 2 Dicanio, Giovanni (March 2017). "Simplify Safe Array Programming in C++ with CComSafeArray". MSDN Magazine. Vol. 32, no. 3. Microsoft. pp. 40–47. ISSN 1528-4859. Wikidata Q140400185.
  24. Troelsen (2000), pp. 166–170.
  25. Microsoft MSDN: _com_ptr_t Class
  26. Microsoft MSDN: _bstr_t Class
  27. Microsoft MSDN: _variant_t Class
  28. Microsoft MSDN: _com_error Class

Bibliography

[edit]

Further reading

[edit]
[edit]