1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#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 |
Partager