Sdílet prostřednictvím


Implementing IUnknown

All event interfaces need to implement the IUnknown interface even though the CEventBase base class implements it as well. The reason for this is that the IUnknown::QueryInterface method, as implemented by the base class, does not know about the interfaces of the derived classes; this means that each derived class must supply its own version of the IUnknown::QueryInterface method (overriding the base class method). And since the methods for an interface cannot be made virtual, it is necessary to implement all the methods on the IUnknown interface (rather than relying on the base class implementation to handle the method calls). In TextInterpreter, the derived implementations of the IUnknown::Add and IUnknown::Release methods simply call the base class implementation to do the work.

To simplify this need to support the IUnknown interface, two macros are created to automate the addition of the IUnknown methods.

To implement IUnknown

  1. Open the EventBase.h file and insert the following macro definition after the include lines:

    // IUnknown methods declaration macro
    #define DECLARE_IUNKNOWN \
        STDMETHOD_(ULONG, AddRef)(void); \
        STDMETHOD_(ULONG, Release)(void); \
        STDMETHOD(QueryInterface)(REFIID riid, LPVOID *ppvObj);
    
  2. Also in EventBase.h, add the following bold lines to the CEngineCreateEvent class after the declaration of ~CEngineCreateEvent(void):

        ////////////////////////////////////////////////////////////
        // IUnknown methods
        DECLARE_IUNKNOWN
    
  3. Open the EventBase.cpp file and insert the following macro definition after the last #include statement:

    // IUnknown methods implementation macro
    #define IMPLEMENT_IUNKNOWN(T, I) \
    ULONG T::AddRef(void) { return CEventBase::AddRef(); } \
    ULONG T::Release(void) { return CEventBase::Release(); } \
    HRESULT T::QueryInterface(REFIID riid, LPVOID *ppvObj) { \
        if (IID_##I == riid) { \
            *ppvObj = (I *)(this); AddRef(); return S_OK; \
        } \
        return CEventBase::QueryInterface(riid, ppvObj); \
    } 
    
  4. At the end of EventBase.cpp, add the following bold lines (right after the definition for CEngineCreateEvent::GetEngine):

    //////////////////////////////////////////////////////////////////////////////
    // IUnknown
    IMPLEMENT_IUNKNOWN(CEngineCreateEvent, IDebugEngineCreateEvent2)
    
  5. Build the project to make sure there are no errors.

See Also

Tasks

Deriving the Startup Event Classes