HOWTO: Override an Interface in an MFC Application

Last reviewed: March 5, 1998
Article ID: Q141277
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2, 4.0, 5.0

SUMMARY

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 doesn't deal with overriding the IDispatch implementation as this is a special case. The following article demonstrates how to override IDispatch in MFC:

   ARTICLE-ID: Q140616
   TITLE     : MFCDISP: Replacing MFC's IDispatch implementation

MORE INFORMATION

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_xMyOleObject.AddRef();
          }
    
          STDMETHODIMP_(ULONG) CIOleOverCtrl::XMyOleObject::Release()
          {
              METHOD_MANAGE_STATE(CIOleOverCtrl, MyOleObject)
              ASSERT_VALID(pThis);
    
              return pThis->m_xMyOleObject.Release ();
          }
    
          STDMETHODIMP CIOleOverCtrl::XMyOleObject::QueryInterface(
              REFIID iid, LPVOID far* ppvObj)
          {
              METHOD_MANAGE_STATE(CIOleOverCtrl, MyOleObject)
              ASSERT_VALID(pThis);
    
              return pThis->m_xMyOleObject.QueryInterface ( iid,  ppvObj);
          }
    
          STDMETHODIMP
          CIOleOverCtrl::XMyOleObject::SetClientSite(LPOLECLIENTSITE
          pClientSite)
          {
              METHOD_MANAGE_STATE(CIOleOverCtrl, MyOleObject)
                 ASSERT_VALID(pThis);
    
              return pThis->m_xMyOleObject.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.

REFERENCES

Technical Notes #38 and #39.

Keywords          : MfcOLE
Technology        : kbMfc kbole
Version           : Winnet:2.0,2.1,2.2,4.0,5.0
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 5, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.