Click here to Skip to main content
15,880,891 members
Articles / Desktop Programming / MFC
Article

Sinking connection points in C++ objects

Rate me:
Please Sign up or sign in to vote.
1.80/5 (2 votes)
5 Jun 2002CPOL5 min read 131.9K   1.2K   44   24
Sometimes it would be nice to be able to use a simple C++ object as a sink for Connection Point notifications. This article shows you how.

The following source was built using Visual Studio 6.0 SP5 and Visual Studio .Net. You need to have the Microsoft NetMeeting SDK installed. This is available from here or as part of recent versions of the Microsoft Platform SDK.

Sinking connection points in C++ objects.

Many COM objects provide events to their clients by means on Connection Points. A Connection Point is a generalised method of allowing clients to register a COM interface so that the object can notify them of things that they may be interested in. The problem is, sometimes you will be developing code that simply uses COM objects and doesn't implement any COM objects itself, yet you still need to be able to register a COM object to receive notifications via a Connection Point. This article shows you how you can use Connection Points and sink events from them without needing to use ATL or MFC or expose real COM objects from your application.

Registering for notifications

Suppose we want to use a COM object that provides notifications via Connection Points, the NetMeeting Conference Manager object for example. Our application will create an instance of the Conference Manager object and manipulate it via its interface, we will monitor the events it generates by registering a connection point to receive events generated by the INmManagerNotify interface. The INmManagerNotify interface is shown below:

interface INmManagerNotify : IUnknown
{
   typedef [unique] INmManagerNotify *LPNMMANAGERNOTIFY;

   HRESULT NmUI(
      [in] CONFN uNotify);

   HRESULT ConferenceCreated(
      [in] INmConference *pConference);

   HRESULT CallCreated(
      [in] INmCall *pCall);
}

We don't want to use MFC or ATL and the resulting program will be a console application using straight C++ and COM client APIs. Our problem is that our application simply uses COM, it doesn't expose any COM functionality itself and we don't want to add the complexity of being able to serve real COM objects simply so that we can consume events from a Connection Point. What we'd like to do is simply implement the methods of the notification interface and ask the Conference Manager object to call us when notifications occur without worrying about the necessary plumbing.

A reusable, self contained, notification class

The work involved to hook up a connection point is fairly straight forward and MFC and ATL could both provide some help if we were using them, but since we're not we have to do the work ourselves. Given the IUnknown of a connectable object we need to obtain a pointer to its IConnectionPointContainer interface and from that obtain a pointer to the required connection point interface. Once we have that, we simply call the Advise() member function, pass our sink interface and keep hold of the cookie that is returned as we need it to disconnect. If we wrap this work in a class we can make sure that we disconnect in the destructor. The class might look something like this:

class CNotify
{
   public :

      CNotify();
      ~CNotify();

      void Connect(
         IUnknown *pConnectTo,
         REFIID riid,
         IUnknown *pIUnknown);

      void Disconnect();

      bool Connected() const;

      private :

         DWORD m_dwCookie;
         IUnknown *m_pIUnknown;
         IConnectionPoint *m_pIConnectionPoint;
         IConnectionPointContainer *m_pIConnectionPointContainer;
};

Connecting can be as simple as calling Connect() and passing the connectable object, the IID of the connection point that we wish to connect to and our sink interface. Disconnecting is equally easy.

The class above is useful but it doesn't solve our problem. We still need to have a COM object to pass as our sink interface and we don't have one, or want to build one. However, it's relative easy to put together a template class that's derived from our CNotify class and fills in the missing functionality. The template looks something like this:

template <class NotifyInterface, const GUID *InterfaceIID> class TNotify :
   private CNotify,
   public NotifyInterface
{
   public :

      void Connect(IUnknown *pConnectTo)
      {
         CNotify::Connect(pConnectTo, *InterfaceIID, this);
      }

      using CNotify::Disconnect;
      using CNotify::Connected;

      // IUnknown methods
      ULONG STDMETHODCALLTYPE AddRef()
      {
         return 2;
      }

      ULONG STDMETHODCALLTYPE Release()
      {
         return 1;
      }

      HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, PVOID *ppvObj)
      {
         HRESULT hr = S_OK;

         if (riid == IID_IUnknown)
         {
            *ppvObj = (IUnknown *)this;
         }
         else if (riid == *InterfaceIID)
         {
            *ppvObj = (NotifyInterface *)this;
         }
         else
         {
            hr = E_NOINTERFACE;
            *ppvObj = NULL;
         }

         return hr;
     }
};

TNotify is used as a base class for your notification sink object, it derives from the sink interface that you want to implement and provides the IUnknown methods for you. You simply have to implement the sink interface methods and then call Connect() and pass the connectable object that you wish to connect to. Your sink object might look like this:

class MyConferenceManager : private TNotify<INmManagerNotify, &IID_INmManagerNotify>
{
   public :

      HRESULT STDMETHODCALLTYPE NmUI(CONFN confn)
      {
         return S_OK;
      }

      HRESULT STDMETHODCALLTYPE ConferenceCreated(INmConference *pConference)
      {
         return S_OK;
      }

      HRESULT STDMETHODCALLTYPE CallCreated(INmCall *pCall)
      {
         return S_OK;
      }
};

You need to be aware of a few things when using your sink object. The first is that the COM object that is constructed for you by the TNotify base class expects to reside on the stack, or, at least, it doesn't expect COM to manage its lifetime (notice the implementation of AddRef() and Release()). Secondly the sink object maintains references to the connectable object's interfaces, so that it can disconnect when you call Disconnect() or, if you don't, when it gets destroyed. Finally, since the sink object could make COM calls when it is destroyed you need to make sure that COM is still initialised at that point - so no calling CoUninitialize() before you destroy your sink object.

The example code

The example code is slightly more complex than that shown above. Because of all the type mapping that you tend to have to do to call COM methods from straight C++ we tend to wrap the COM objects in thin C++ wrappers. We do this with the interface that we obtain for the INmManager object and we use the same wrapper to sink the events. This leaves us with a single C++ object that exposes both the INmManager interface which translates the argument types to C++ friendly types and throws exceptions on method failures, and also exposes the INmManagerNotify interface to notify us of events that occur on the object. As you'll see, creating and using the NetMeeting Conference Manager COM object and wiring it up to receieve notification events is as simple this:

   class MyConferenceManager : public CNmConferenceManager
   {
      public :
   
         // implement some or all of the INmManagerNotify methods to do 
         // stuff when the notifications happen...
   };

...

   if (MyConferenceManager::IsNmInstalled())
   {
      MyConferenceManager confManager;

      ULONG options = NM_INIT_NORMAL;
      ULONG capabilities = NMCH_ALL;

      confManager.Initialize(options, capabilities);

... 

Since the CNmConferenceManager object implements all of the notification interface and just does nothing, i.e. it returns S_OK to all methods, our derived class can choose to simply override the notifications that it wants to handle. Creating our object initialises COM, so that COM remains initialised until our object is no more, creates the NetMeeting Conference Manager COM object and connects up the notification sink. We can then simply call methods on the object and recieve notifications back.

Revision history

  • 30th May 2002 - Initial revision.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) JetByte Limited
United Kingdom United Kingdom
Len has been programming for over 30 years, having first started with a Sinclair ZX-80. Now he runs his own consulting company, JetByte Limited and has a technical blog here.

JetByte provides contract programming and consultancy services. We can provide experience in COM, Corba, C++, Windows NT and UNIX. Our speciality is the design and implementation of systems but we are happy to work with you throughout the entire project life-cycle. We are happy to quote for fixed price work, or, if required, can work for an hourly rate.

We are based in London, England, but, thanks to the Internet, we can work 'virtually' anywhere...

Please note that many of the articles here may have updated code available on Len's blog and that the IOCP socket server framework is also available in a licensed, much improved and fully supported version, see here for details.

Comments and Discussions

 
QuestionMissing template arguments? Pin
mxcl9-Aug-06 1:35
mxcl9-Aug-06 1:35 
AnswerRe: Missing template arguments? Pin
Len Holgate9-Aug-06 11:31
Len Holgate9-Aug-06 11:31 
GeneralRe: Missing template arguments? Pin
mxcl9-Aug-06 23:23
mxcl9-Aug-06 23:23 
GeneralHello , Pin
belkhiri6-Apr-06 8:22
belkhiri6-Apr-06 8:22 
GeneralIDispatch Pin
OlaSjöstedt30-Mar-04 2:15
OlaSjöstedt30-Mar-04 2:15 
GeneralRe: IDispatch Pin
OlaSjöstedt4-Apr-04 22:24
OlaSjöstedt4-Apr-04 22:24 
I can reply myself now. Yes it does work for an IDispatch inherited interface. I have to override the Invoke member for every sink I create though. My default Invoke uses ITypeInfo->Invoke, but it fails with DISP_E_MEMBERNOTFOUND. A bit inconvenient to implement the Invoke myself for each sink, but it works anyway.
GeneralRe: IDispatch Pin
Len Holgate5-Apr-04 7:56
Len Holgate5-Apr-04 7:56 
GeneralRe: IDispatch Pin
OlaSjöstedt5-Apr-04 23:49
OlaSjöstedt5-Apr-04 23:49 
GeneralRe: IDispatch Pin
OlaSjöstedt5-Apr-04 23:51
OlaSjöstedt5-Apr-04 23:51 
GeneralI maybe wrong Pin
igor196025-Sep-03 11:44
igor196025-Sep-03 11:44 
GeneralRe: I maybe wrong Pin
Len Holgate25-Sep-03 12:16
Len Holgate25-Sep-03 12:16 
GeneralRe: I maybe wrong Pin
igor196025-Sep-03 12:18
igor196025-Sep-03 12:18 
QuestionHow about passing C++ object(SAFEARRAY) as Event parameter? Pin
i--13-Aug-03 2:01
i--13-Aug-03 2:01 
AnswerRe: How about passing C++ object(SAFEARRAY) as Event parameter? Pin
Carsten Zeumer28-Oct-03 4:09
Carsten Zeumer28-Oct-03 4:09 
GeneralOut-of-proc server Pin
Sidra Ahmed18-Feb-03 20:32
sussSidra Ahmed18-Feb-03 20:32 
GeneralRe: Out-of-proc server Pin
Len Holgate19-Feb-03 8:27
Len Holgate19-Feb-03 8:27 
GeneralAdvice required Pin
anonymous129-Jan-03 1:05
anonymous129-Jan-03 1:05 
GeneralRe: Advice required Pin
Len Holgate9-Jan-03 10:04
Len Holgate9-Jan-03 10:04 
Generalit does not deal with dispinterface Pin
MaximE26-Sep-02 8:46
MaximE26-Sep-02 8:46 
GeneralRe: it does not deal with dispinterface Pin
Len Holgate26-Sep-02 12:48
Len Holgate26-Sep-02 12:48 
GeneralRe: it does not deal with dispinterface Pin
MaximE28-Sep-02 13:22
MaximE28-Sep-02 13:22 
GeneralTruly excellent you seem like the person to ask! Pin
Bo Carleon22-Jul-02 13:51
sussBo Carleon22-Jul-02 13:51 
GeneralRe: Truly excellent you seem like the person to ask! Pin
Len Holgate22-Jul-02 21:29
Len Holgate22-Jul-02 21:29 
GeneralJust a bit more help, please! Pin
Jason Troitsky (was Hattingh)16-Jun-02 22:49
Jason Troitsky (was Hattingh)16-Jun-02 22:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.