Microsoft KB Archive/181845

= How to create a sink interface in a MFC-based COM client =

Article ID: 181845

Article Last Modified on 10/21/2005

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li> <li>Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

-

<div class="notice_section">

This article was previously published under Q181845

<div class="idea_section">

Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

<div class="summary_section">

SUMMARY
Microsoft Foundation Classes (MFC) has wizard support to add sink interfaces for ActiveX controls. However, this support does not extend to other COM servers. This article describes how to add a sink interface in an MFC client for source interfaces described by COM servers. Please note that this article applies to source interfaces, which are dispinterfaces, or dual interfaces with events being called through IDispatch::Invoke.

<div class="moreinformation_section">

MORE INFORMATION
Here are the steps to add a sink interface to a COM client: <ol> <li>Using the Class Wizard, add a CCmdTarget derived object (for example, CMySink) with automation support. In the Class Wizard, select the Automation option rather than "Createable by type ID" option when adding this class.

Note If you are using Visual Studio .NET, do the following:

On the Project menu, select Add Class. In Add Class Dialog, select MFC Class. In MFC Class Wizard, on the Names page, under the base class, select CCmdTarget, and then under select support for automation, select Automation.</li> <li>In the interface map, change the IID (the second parameter in the INTERFACE_PART macro) so that it is the IID of the source interface (usually the interface with the default and/or source attribute in the server's .idl file). The .idl file can be seen by viewing the typelib in the OLE/COM Object Viewer.</li> <li> In the DISPATCH_MAP of CMySink class, add a DISP_FUNCTION_ID macro for each of the events defined in the source interface that you want to handle. For example: BEGIN_DISPATCH_MAP(CMySink, CCmdTarget) DISP_FUNCTION_ID(CMySink,"Quit",2,OnObjQuit,VT_EMPTY,VTS_I4 VTS_I4) END_DISPATCH_MAP The code above is an entry for handling the Quit event with a DISPID 2, which takes two long parameters and returns a void. OnObjectQuit is a CMySink member function that takes two longs and returns a void. This function must be added manually and is called when the COM server fires a Quit event. </li> <li> Now you have hooked up the sink interface with the server so that you can start receiving events. To do this, call the AfxConnectionAdvise function once the server object is created. For example: //Instantiate the sink class and hold a pointer to it. m_pSink = new CMySink;

//Get a pointer to sinks IUnknown, no AddRef. CMySink implements only //dispinterface and the IUnknown and IDispatch pointers will be same. LPUNKNOWN pUnkSink = m_pSink->GetIDispatch(FALSE);

//Establish a connection between source and sink. //m_pUnkSrc is IUnknown of server obtained by CoCreateInstance. //m_dwCookie is a cookie identifying the connection, and is needed //to terminate the connection. AfxConnectionAdvise(m_pUnkSrc, IID_MYEVENT, pUnkSink, FALSE,     &m_dwCookie); </li> <li> When you have finished using the server object you need to terminate the connection before releasing the server object. You do this by calling the AfxConnectionUnadvise function. For example: //Get a pointer to sinks IUnknown, no AddRef. LPUNKNOWN pUnkSink = m_pSink->GetIDispatch(FALSE);

//Terminate a connection between source and sink. //m_pUnkSrc is IUnknown of server obtained by CoCreateInstance. //m_dwCookie is a value obtained through AfxConnectionAdvise. AfxConnectionUnadvise(m_pUnkSrc, IID_MYEVENT, pUnkSink, FALSE,  m_dwCookie); </li></ol>

Because CMySink was created on the heap, make sure you delete it to avoid memory leaks.

<div class="references_section">