What's the purpose of IUnknown member functions in END_COM_MAP?

左心房为你撑大大i 提交于 2019-12-11 15:18:35

问题


ATL END_COM_MAP macro is defined as follows:

#define END_COM_MAP() \
    __if_exists(_GetAttrEntries) {{NULL, (DWORD_PTR)_GetAttrEntries, _ChainAttr }, }\
    {NULL, 0, 0}}; return _entries;} \
    virtual ULONG STDMETHODCALLTYPE AddRef( void) throw() = 0; \
    virtual ULONG STDMETHODCALLTYPE Release( void) throw() = 0; \
    STDMETHOD(QueryInterface)(REFIID, void**) throw() = 0;

It is intended to be used within definition of classes inherited from COM interfaces, for example:

class ATL_NO_VTABLE CMyClass :
    public CComCoClass<CMyClass, &MyClassGuid>,
    public CComObjectRoot,
    public IMyComInterface
{
public:
    BEGIN_COM_MAP( CMyClass )
        COM_INTERFACE_ENTRY( IMyComInterface)
    END_COM_MAP()
};

This means that QueryInterface(), AddRef() and Release() are declared as pure virtual in this class. Since I don't define their implementation this class should be uncreatable. Yet ATL successfully instantiates it.

How does it work and why are those IUnknown member functions redeclared here?


回答1:


It's been a while since I used ATL but, IIRC, what ends up being instantiated is not CMyClass, but CComObject<CMyClass>.

CComObject implements IUnknown and inherits from its template parameter.

Edit: The "Fundamentals of ATL COM Objects" page on MSDN nicely illustrates what's going on.



来源:https://stackoverflow.com/questions/1729834/whats-the-purpose-of-iunknown-member-functions-in-end-com-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!