Chapter 6 -- Fundamentals of ATL COM Objects

[Previous] [Next]

Chapter 6

Creating COM objects in ATL takes a little getting used to. The Object Wizard is a great place to start, but the generated code isn't exactly intuitive. In this chapter, we'll explain the generated code and explore the following classes, which form the foundation of ATL COM object implementations:

  • CComThreadModel and CComCriticalSection
  • CComObjectRoot
  • CComObject
  • IDispatchImpl

We'll also see how these classes help with ATL debugging.

Let's start by looking at the header file for a simple object, CSimple1, as generated by the Object Wizard using the wizard's default settings:

 // Simple1.h : Declaration of the CSimple1 #ifndef _ _SIMPLE1_H_ #define _ _SIMPLE1_H_ #include "resource.h"       // Main symbols /////////////////////////////////////////////////////////////// // CSimple1 class ATL_NO_VTABLE CSimple1 :      public CComObjectRootEx<CComSingleThreadModel>,     public CComCoClass<CSimple1, &CLSID_Simple1>,     public IDispatchImpl<ISimple1, &IID_ISimple1,         &LIBID_FUNOBJECTSLib> { public:     CSimple1()     {     } DECLARE_REGISTRY_RESOURCEID(IDR_SIMPLE1) DECLARE_PROTECT_FINAL_CONSTRUCT() BEGIN_COM_MAP(CSimple1)     COM_INTERFACE_ENTRY(ISimple1)     COM_INTERFACE_ENTRY(IDispatch) END_COM_MAP() // ISimple1 public: }; #endif // _ _SIMPLE1_H_ 

The header file shown here is all the application-specific code you need to implement a functional COM object that supports IUnknown, IDispatch, and the dual interface generated by the Object Wizard, ISimple1. Of course, a fair amount of ATL code is under the sheets. Our CSimple1 object derives from several ATL classes to inherit implementations of IUnknown and IDispatch and to properly handle concurrency issues and aggregation. Figure 6-1 shows where the CSimple1 class fits into the ATL inheritance tree for the object.

click to view at full size.

Figure 6-1. The CSimple1 class hierarchy.

In general, the CComObjectRootEx and CComObject classes work together to implement IUnknown for the object, although CComObject doesn't enter the picture until the object is actually created. CComCoClass defines the class factory behavior, which we discuss in detail in Chapter 7, along with the object map and registration. IDispatchImpl supplies the IDispatch part of the ISimple1 dual interface. The individual ISimple1 methods are left for us to implement.

As we'll soon see, many of the class variations that ATL implements for us depend on the choices we make for threading-model and aggregation support. First, though, we need to take a brief look at the critical-section and threading-model classes, which are used by the root classes.

Inside Atl
Inside ATL (Programming Languages/C)
ISBN: 1572318589
EAN: 2147483647
Year: 1998
Pages: 127 © 2008-2017.
If you may any questions please contact us: