Creating the CEventBase Class

The CEventBase base class implements the IDebugEvent2 interface, which has IDebugEvent2::GetAttributes as its only method. All the debug event classes are derived from the CEventBase class. These subclasses also each inherit a particular interface specific to each event type. In this implementation, the CEventBase constructor for an event stores both the attributes of an event and the interface identifier (IID) of the particular event interface.

To create the CEvent base class

  1. In Solution Explorer, right-click the TextInterpreter project and click Add Class.

  2. Click Generic on the left and Generic C++ Class on the right, and then click Add.

  3. Type CEventBase for the Class name and IDebugEvent2 for the Base class. Make sure that Access is set to public and the Virtual Destructor option is selected. Click Finish to add the class.

  4. In Class View, right-click the new CEventBase class, select Add Variable, and add a variable with the Variable name  m_dwAttrib, the Variable type DWORD, and an Access type of protected.

  5. Add another variable to the CEventBase class with the Variable name  m_riid, a Variable type IID, and an Access type of protected.

  6. Add a third variable to the CEventBase class with the Variable name  m_iRefCount, a Variable type of ULONG, and an Access type of protected.

  7. Open the EventBase.h file and add two arguments to the CEventBase constructor declaration so it looks like this:

        CEventBase(REFIID iid, DWORD dwAttrib);
    
  8. In EventBase.h, insert the following bold lines. These are the declarations for the IUnknown and IDebugEvent2 interfaces.

    virtual ~CEventBase(void)
    
        //////////////////////////////////////////////////////////// 
        // IUnknown methods 
        STDMETHOD_(ULONG, AddRef)(void); 
        STDMETHOD_(ULONG, Release)(void); 
        STDMETHOD(QueryInterface)(REFIID riid, LPVOID *ppvObj); 
    
        //////////////////////////////////////////////////////////// 
        // IDebugEvent2 methods 
        STDMETHOD(GetAttributes)(DWORD* pdwAttrib); 
    
  9. Open the EventBase.cpp file and modify the CEventBase::CEventBase constructor to look like the following:

    CEventBase::CEventBase(REFIID iid, DWORD dwAttrib)
    : m_dwAttrib(dwAttrib) 
    , m_iRefCount(0) 
    , m_riid(iid) 
    {
    }
    
  10. In EventBase.cpp, add the following bold lines to the end of the file:

    CEventBase::~CEventBase(void)
    {
    }
    
    ////////////////////////////////////////////////////////////////////////////// 
    // IUnknown methods 
    ULONG CEventBase::AddRef(void) 
    { 
        return ++m_iRefCount; 
    } 
    
    ULONG CEventBase::Release(void) 
    { 
        m_iRefCount--; 
        if (m_iRefCount == 0) { 
            delete this; 
            return 0; 
        } 
        return m_iRefCount; 
    } 
    
    HRESULT CEventBase::QueryInterface(REFIID riid, LPVOID *ppvObj) 
    { 
        if (riid == IID_IUnknown) 
            *ppvObj = (IUnknown*) this; 
        else if (riid == IID_IDebugEvent2) 
            *ppvObj = (IDebugEvent2*) this; 
        else return E_NOINTERFACE; 
    
        AddRef(); 
        return S_OK;  
    } 
    
    ////////////////////////////////////////////////////////////////////////////// 
    // IDebugEvent2 methods 
    HRESULT CEventBase::GetAttributes(DWORD* pdwAttrib) 
    { 
        *pdwAttrib = m_dwAttrib; 
        return S_OK; 
    } 
    
  11. Build the project to make sure there are no errors.

See Also

Tasks

Creating the Event Classes