Help and Support

DOC: Technical Note 38: MFC/OLE IUnknown Implementation

Article ID:145746
Last Review:November 21, 2006
Revision:2.1
This article was previously published under Q145746
On This Page

SUMMARY

Technical Note 38 "TN038: MFC/OLE IUnknown Implementation" contains two errors. First, it describes how to implement standard and custom interface functions for an OLE COM object using the MFC Interface Map macros, but the code is incorrect. Additionally, the article explains how to aggregate an object, but the code in the interface map is also incorrect.

The problems were corrected in Visual C++ 4.1.

Back to the top

MORE INFORMATION

First Code Problem

For the implementation of the interface functions, two sets of sample code are included, the first set is correct. The code immediately following the discussion of the Interface Map macros is the code in error. The code illustrates the implementation of the IUnknown functions and custom interface functions for the interface being created using the MFC Interface Maps. The sample code in error is as follows:
   ULONG FAR EXPORT CEditPrintObj::CEditObj::AddRef()
   {
       METHOD_PROLOGUE(CEditPrintObj, EditObj)
       return pThis->ExternalAddRef();
   }

   ULONG FAR EXPORT CEditPrintObj::CEditObj::Release()
   {
       METHOD_PROLOGUE(CEditPrintObj, EditObj)
       return pThis->ExternalRelease();
   }

   HRESULT FAR EXPORT CEditPrintObj::CEditObj::QueryInterface(
       REFIID iid, void FAR* FAR* ppvObj)
   {
       METHOD_PROLOGUE(CEditPrintObj, EditObj)
       return (HRESULT)pThis->ExternalQueryInterface(&iid,
                     ppvObj);
   }

   void FAR EXPORT CEditPrintObj::CEditObj::EditObject()
   {
       METHOD_PROLOGUE(CEditPrintObj, EditObj)
       // code to "Edit" the object, whatever that     // means...
   }
				
All of the function signatures are incorrect. CEditObj should actually be XEditObj. The BEGIN_INTERFACE_PART and END_INTERFACE_PART macros create a nested class inside the object class definition, and assign a name to the nested class. The name of the nested class is created by prepending an X to the first argument of the BEGIN_INTERFACE_PART macro, which should be the name of your custom interface. Therefore, if you are going to implement the functions in the nested classes that were created for you by the BEGIN_INTERFACE_PART and END_INTERFACE_PART macros, the nested classes name would always begin with an X. This is to distinguish the nested class from global classes (which typically start with C) and interface classes (which typically start with I).

To implement a member of the above described nested class correctly, you need to implement the AddRef shown above as:
   ULONG FAR EXPORT CEditPrintObj::XEditObj::AddRef()
   {
      METHOD_PROLOGUE(CEditPrintObj, EditObj)
      return pThis->ExternalAddRef();
   }
				

Back to the top

Second Code Problem

The second problem is in the discussion of aggregation. In the second step, the instructions say, "Include an INTERFACE_AGGREGATE macro in your interface map, which refers to the member variable by name." However, the code doesn't do that. Here is the incorrect code:
   BEGIN_INTERFACE_MAP(CAggrExample, CCmdTarget)
      // native "INTERFACE_PART" entries go here
      INTERFACE_AGGREGATE(CAggrExample, m_lpTypeInfo)
   END_INTERFACE_MAP()
				
The variable m_lpTypeInfo should be m_lpAggrInner. The code should look like this:
   BEGIN_INTERFACE_MAP(CAggrExample, CCmdTarget)
      // native "INTERFACE_PART" entries go here
      INTERFACE_AGGREGATE(CAggrExample, m_lpAggrInner)
   END_INTERFACE_MAP()
				

Back to the top


APPLIES TO
Microsoft Foundation Class Library 4.2, when used with:
  Microsoft Visual C++ 4.0 Standard Edition

Back to the top

Keywords: 
kbautomation kbbug kbdocerr kbdocfix kbfix kbinprocsvr kbvc410fix KB145746

Back to the top

Article Translations

 

Other Support Options

  • Contact Microsoft
    Phone Numbers, Support Options and Pricing, Online Help, and more.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.