Click here to Skip to main content
11,504,452 members (69,054 online)
Click here to Skip to main content
Add your own
alternative version

Understanding COM Event Handling

, 9 Dec 2004 CPOL 366.8K 4.6K 195
Learn the fundamental principles of COM Event Handling via a C++ template class that allows for generic handling of dispinterface COM events.
teventhandler_src.zip
TEventHandler
EventFiringObject
TestClient
EventFiringObject.def
EventFiringObject.dsp
EventFiringObject.dsw
EventFiringObject.rgs
EventFiringObject.tlb
EventFiringObjectps.def
EventFiringObjectps.mk
EventFiringObject.tlh
EventFiringObject.tli
res
TestClient.clw
TestClient.dsp
TestClient.dsw
TestClient.ico
#ifndef TEVENT_HANDLER_H
  #define TEVENT_HANDLER_H

#include <windows.h>
//#include <SHLOBJ.H>
//#include <memory>
//#include <string>

namespace TEventHandlerNamespace
{
// Generic event handler template class (especially useful (but not limited to) for non-ATL clients).
template <class event_handler_class, typename device_interface, typename device_event_interface>
class TEventHandler : IDispatch
{
  friend class class_event_handler;

  typedef HRESULT (event_handler_class::*parent_on_invoke)
  (
    TEventHandler<event_handler_class, device_interface, device_event_interface>* pthis,
	DISPID dispidMember, 
	REFIID riid,
	LCID lcid, 
	WORD wFlags, 
	DISPPARAMS* pdispparams, 
	VARIANT* pvarResult,
	EXCEPINFO* pexcepinfo, 
	UINT* puArgErr
  );

public :
  TEventHandler
  (
    event_handler_class& parent,
	device_interface* pdevice_interface,  // Non-ref counted.
	parent_on_invoke parent_on_invoke_function
  ) :
    m_cRef(1),
    m_parent(parent),
	m_parent_on_invoke(parent_on_invoke_function),
	m_pIConnectionPoint(0),
	m_dwEventCookie(0)
  {
    SetupConnectionPoint(pdevice_interface);
  }
	   
  ~TEventHandler()
  {
	// Call ShutdownConnectionPoint() here JUST IN CASE connection points are still 
	// alive at this time. They should have been disconnected earlier.
    ShutdownConnectionPoint();
  }

  STDMETHOD_(ULONG, AddRef)()
  {
	InterlockedIncrement(&m_cRef);
	
	return m_cRef;  
  }
	   
  STDMETHOD_(ULONG, Release)()
  {
	InterlockedDecrement(&m_cRef);
	
	if (m_cRef == 0)
	{
		delete this;
		return 0;
	}
	
	return m_cRef;
  }
	   
  STDMETHOD(QueryInterface)(REFIID riid, void ** ppvObject)
  {
	if (riid == IID_IUnknown)
	{
		*ppvObject = (IUnknown*)this;
		AddRef();
		return S_OK;
	}
	
	if ((riid == IID_IDispatch) || (riid == __uuidof(device_event_interface)))
	{
		*ppvObject = (IDispatch*)this;
		AddRef();
		return S_OK;
	}
	
	return E_NOINTERFACE;
  }
	   
  STDMETHOD(GetTypeInfoCount)(UINT* pctinfo)
  {
	return E_NOTIMPL;
  }
	   
  STDMETHOD(GetTypeInfo)(UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
  {
	return E_NOTIMPL;
  }
	   
  STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames,
		   LCID lcid, DISPID* rgdispid)
  {
	return E_NOTIMPL;
  }
	   
  STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid,
		   LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
		   EXCEPINFO* pexcepinfo, UINT* puArgErr)
  {
	return (m_parent.*m_parent_on_invoke)(this, dispidMember, riid, lcid, wFlags, pdispparams, pvarResult, pexcepinfo, puArgErr);
  }
	   
protected :
  LONG						m_cRef;

  // Pertaining to the owner of this object.
  event_handler_class&		m_parent;  // Non-reference counted. This is to prevent circular references.

  // Pertaining to connection points.
  IConnectionPoint*			m_pIConnectionPoint;  // Ref counted of course.
  DWORD						m_dwEventCookie;
  parent_on_invoke			m_parent_on_invoke;

  void SetupConnectionPoint(device_interface* pdevice_interface)
  {
    IConnectionPointContainer*	pIConnectionPointContainerTemp = NULL;
    IUnknown*					pIUnknown = NULL;

    // QI this object itself for its IUnknown pointer which will be used 
    // later to connect to the Connection Point of the device_interface object.
    this -> QueryInterface(IID_IUnknown, (void**)&pIUnknown);

    if (pIUnknown)
    {
      // QI the pdevice_interface for its connection point.
      pdevice_interface -> QueryInterface (IID_IConnectionPointContainer, (void**)&pIConnectionPointContainerTemp);
	
      if (pIConnectionPointContainerTemp)
      {
	    pIConnectionPointContainerTemp -> FindConnectionPoint(__uuidof(device_event_interface), &m_pIConnectionPoint);
	    pIConnectionPointContainerTemp -> Release();
	    pIConnectionPointContainerTemp = NULL;
      }
	
      if (m_pIConnectionPoint)
      {
	    m_pIConnectionPoint -> Advise(pIUnknown, &m_dwEventCookie);
      }
	
      pIUnknown -> Release();
	  pIUnknown = NULL;
    }
  }

public :

  void ShutdownConnectionPoint()
  {
    if (m_pIConnectionPoint)
    {
	  m_pIConnectionPoint -> Unadvise(m_dwEventCookie);
	  m_dwEventCookie = 0;
	  m_pIConnectionPoint -> Release();
	  m_pIConnectionPoint = NULL;
    }
  }
};

};





#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Lim Bio Liong
Web Developer
Singapore Singapore
Lim Bio Liong is a Specialist at a leading Software House in Singapore.

Bio has been in software development for over 10 years. He specialises in C/C++ programming and Windows software development.

Bio has also done device-driver development and enjoys low-level programming. Bio has recently picked up C# programming and has been researching in this area.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 10 Dec 2004
Article Copyright 2004 by Lim Bio Liong
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid