How to override an interface in an MFC application by using Visual C++

This article was previously published under Q141277
This article has been archived. It is offered "as is" and will no longer be updated.
Note Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

Note Microsoft Visual C++ 2005 supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model.
In an MFC application, you can override existing interfaces in a class as well as provide additional interfaces. Overriding an interface in this case is synonymous with replacing an interface. The example in this article illustrates how to override an interface in a class while preserving the original interface implementation so that it can be delegated to by the new interface implementation.

This article does not deal with overriding the IDispatch implementation, as this is a special case. For more information about how to override IDispatch in MFC, click the following article number to view the article in the Microsoft Knowledge Base:
140616 Replacing MFC IDispatch implementation
The following steps will override the IOleObject implementation for a default OLE Control generated by the Control Wizard.

  1. To add the declaration of the IOleObject implementation to the control, add the following code to the header file for the COleControl-derived class:
          // Interface Maps      protected:           // IOleObject           BEGIN_INTERFACE_PART(MyOleObject, IOleObject)               INIT_INTERFACE_PART(CIOleOverCtrl, MyOleObject)               STDMETHOD(SetClientSite)(LPOLECLIENTSITE);               STDMETHOD(GetClientSite)(LPOLECLIENTSITE*);               STDMETHOD(SetHostNames)(LPCOLESTR, LPCOLESTR);               STDMETHOD(Close)(DWORD);               STDMETHOD(SetMoniker)(DWORD, LPMONIKER);               STDMETHOD(GetMoniker)(DWORD, DWORD, LPMONIKER*);               STDMETHOD(InitFromData)(LPDATAOBJECT, BOOL, DWORD);               STDMETHOD(GetClipboardData)(DWORD, LPDATAOBJECT*);               STDMETHOD(DoVerb)(LONG, LPMSG, LPOLECLIENTSITE, LONG, HWND,                       LPCRECT);               STDMETHOD(EnumVerbs)(IEnumOLEVERB**);               STDMETHOD(Update)();               STDMETHOD(IsUpToDate)();               STDMETHOD(GetUserClassID)(CLSID*);               STDMETHOD(GetUserType)(DWORD, LPOLESTR*);               STDMETHOD(SetExtent)(DWORD, LPSIZEL);               STDMETHOD(GetExtent)(DWORD, LPSIZEL);               STDMETHOD(Advise)(LPADVISESINK, LPDWORD);               STDMETHOD(Unadvise)(DWORD);               STDMETHOD(EnumAdvise)(LPENUMSTATDATA*);               STDMETHOD(GetMiscStatus)(DWORD, LPDWORD);               STDMETHOD(SetColorScheme)(LPLOGPALETTE);           END_INTERFACE_PART(MyOleObject)      DECLARE_INTERFACE_MAP();						
    This adds a nested class XMyOleObject to your control class. Note that these macros declare interface methods including the IUnknown interface methods, so you must implement the IUnknown methods as well.
  2. Add the IOleObject interface to the interface map for the control by adding an INTERFACE_PART macro to the implementation file for the control:
          BEGIN_INTERFACE_MAP(CIOleOverCtrl, COleControl)         INTERFACE_PART(CIOleOverCtrl, IID_IOleObject, MyOleObject)      END_INTERFACE_MAP()						
    Replace CIOleOverCtrl with the name of your control and MyOleObject with the name you chose for the nested class that supports IOleObject.
  3. Implement the interface methods you declared. Add the following code to the implementation file for the control:
          STDMETHODIMP_(ULONG) CIOleOverCtrl::XMyOleObject::AddRef()      {          METHOD_MANAGE_STATE(CIOleOverCtrl, MyOleObject)          ASSERT_VALID(pThis);          return pThis->m_xOleObject.AddRef();      }      STDMETHODIMP_(ULONG) CIOleOverCtrl::XMyOleObject::Release()      {          METHOD_MANAGE_STATE(CIOleOverCtrl, MyOleObject)          ASSERT_VALID(pThis);          return pThis->m_xOleObject.Release ();      }      STDMETHODIMP CIOleOverCtrl::XMyOleObject::QueryInterface(          REFIID iid, LPVOID far* ppvObj)      {          METHOD_MANAGE_STATE(CIOleOverCtrl, MyOleObject)          ASSERT_VALID(pThis);          return pThis->m_xOleObject.QueryInterface ( iid,  ppvObj);      }      STDMETHODIMP      CIOleOverCtrl::XMyOleObject::SetClientSite(LPOLECLIENTSITE      pClientSite)      {          METHOD_MANAGE_STATE(CIOleOverCtrl, MyOleObject)             ASSERT_VALID(pThis);          return pThis->m_xOleObject.SetClientSite ( pClientSite );      }      ...
The rest of the methods follow the same pattern where CIOleOverCtrl is the name of the control, XMyOleObject is the name of the nested class that supports IOleObject, and m_xMyOleObject is calculated by removing the I from the interface being supported and adding m_x.

Note that these methods simply pass the call on to the original IOleObject implementation. However, this is not a requirement; you could add functionality and delegate to the original implementation or not delegate at all.
Technical Notes #38 and #39.

Article ID: 141277 - Last Review: 02/28/2014 08:00:10 - Revision: 3.1

Microsoft Foundation Class Library 4.2

  • kbnosurvey kbarchive kbhowto kbarchitecture kbctrl KB141277