In the table driver in CCOMOBJECTROTOBJECTBASE, the inner implementation of the interface is implemented, ienalQueryInterface (). So when you create an ATL-based COM class, you need to create a mapping table that contains all implementations.
1. Begin_COM_MAP, END_COM_MAP, COM_INTERFACE_ENTRY and COM_ITERFACE_ENTRY2 Macro
ATL provides four macows that Begin_COM_MAP, END_COM_MAP, COM_INTERFACE_ENTRY and COM_INTERFACE_ENTRY2 to create an interface mapping table.
Suppose a class CCLASSA inherits the interface IINTA and IINTB, and the interface mapping table of the class is created as follows:
Class CCLASSA: PUBLIC CCOMOBJECTROOTEX
When CCLASSB inherits IINTC and IINTD, and IINTC and IINTD are inherited from the IdisPatch interface. At this point, if the client program is querying the IDispatch interface, the iDispatch interface pointer returned by QueryInterface will not be able to determine IINTC or IINTD. In this case, you need to specify the default pointing of the IdisPatch interface pointer. COM_INTERFACE_ENTRY2 () macro is used to complete this feature. The following code will default to the IINTD ID interface pointer to the request for the IdisPatch interface.
class CClassB: public CComObjectRootEx
2. Implementation with the interface mapping table
The CClassB Macro expansion in the following code can be obtained with minor streamlining: class CClassB: public CComObjectRootEx
According to the above code, the role of Begin_COM_MAP () is the key to provide a static _GetenTries () method for obtaining a static COM interface mapping table created in this method. Where, when the base class (or interface) is the parent class (or interface) of the Derived class (or interface), the OffsetOfClass (BASE, DERIVED) macro is used to return the Base interface (or class) pointer in the class relative to the Derived pointer. The pointer offset value in the derived category. _ATL_SIMPLEMAPENTRY constant represents the second member DW of the _ATL_INTMAP_ENTRY structure represents the offset value of the base to the Derived pointer. COM_ITERFACE_ENTRY2 The main difference between COM_INTERFACE_ENTRY is the calculation of this offset value. At the same time, the END_COM_MAP () macro also declares the iUnknown's addRef () and release () method as a pure virtual function. As a result, the method of the IunkNown interface will be destined to have subclass of such classes.
3. Conclusion
Each ATL-based COM object must first create a static interface mapping table. The creation of this table is done by begin_com_map, end_com_map, com_interface_entry with com_interface_entry2 four macros. They created an interface mapping table and interface mapping table, while also declaring the iUnknown method as a pure virtual function, which will be implemented by its derived class.