Chapter 4 — Sample Applications
49
FMTestApp
HRESULT EasyAdvise(IUnknown* pUnk)
{
// Make sure the COM object corresponding to pUnk implements
// IProvideClassInfo2 or IPersist*. Call this method to extract info
// about source type library if you specified only 2 parameters to
// IDispEventImpl
HRESULT hr = AtlGetObjectSourceInterface
pUnk, &m_libid, &m_iid, &m_wMajorVerNum,
&m_wMinorVerNum);
// connect the sink and source
hr = DispEventAdvise(pUnk, &m_iid);
return hr;
}
4. Within the CFilterManagerEvents class, define and implement a method
that calls AtlGetObjectSourceInterface() and DispEventUnadvise().
Once
again,
AtlGetObjectSourceInterface()
is called to retrieve
pUnk
, a pointer to the
event source.
DispEventUnadvise()
breaks the connection with the event source
represented by
pUnk
. Once the connection is broken, events will no longer be
routed to the handler functions.
In our C++ sample application, the EasyUnadvise method is included in the
CFilterManagerEvents class:
HRESULT EasyUnadvise(IUnknown* pUnk)
{
AtlGetObjectSourceInterface(pUnk,&m_libid, &m_iid,
&m_wMajorVerNum, &m_wMinorVerNum);
return DispEventUnadvise(pUnk, &m_iid);
}
5. Within the CFilterManagerEvents class, define and implement the
functions that will handle the events that you’ve registered for.
Within the
body of each of these event handlers, insert code to respond in whatever way you
decide to the event that you’re receiving. For example, you may choose to write
out to a log file any messages that you receive from a Log Event. Within the class
definition of our sample application, the event handlers are prototyped as follows:
STDMETHOD(OnError)(long code, BSTR error);
STDMETHOD(OnLog)(long code, BSTR error);
STDMETHOD(OnFinished)(long code, BSTR error);
STDMETHOD(OnPause)(long code, BSTR error);