Your plugin now can load into AIMCC, and from there can call methods on AIMCC objects. But it cannot receive events from AIMCC, such as when a buddy comes online or when a message arrives. To do this your plugin needs to implement the DAccEvents interface, and hook up this event sink to AIMCC.
Implement DAccEvents by adding IDispEventImpl as shown below in the class. In this example we will implement the event OnBeforeImSend which is fired after the user has sent the IM but before the IM is sent to the host.
Hooking up your DAccEvents implementation is a bit trickier, but not much. The first step is to declare a new private member variable in your plugin class of type CComPtr<IAccSession> (we will call ours m_spiSession). This is a smart pointer that will hold onto the IAccSession that is passed into your plugin in IAccPlugin::Init. With this smart pointer, we will call the DispEventAdvise function to connect the DAccEvents implementation with AIMCC; we will do this in the Init method. In the Shutdown method, we will call DispEventUnadvise to disconnect from the event source, again using our smart pointer.
To do this, we add the following red lines of code.
class ATL_NO_VTABLE CMyPlugin :
public IMyPlugin,
public
IAccPlugin,
public IDispEventImpl<1,
CMyPlugin, &__uuidof(DAccEvents),
&LIBID_AccCoreLib, /* wMajor = */ 1,
/* wMinor = */ 0>
{
. . .
STDMETHOD(Init)(IAccSession * piAccSession, IAccPluginInfo*
piPluginInfo)
{
m_spiSession =
piAccSession;
return
DispEventAdvise(m_spiSession);
}
STDMETHOD(Shutdown)()
{
DispEventUnadvise(m_spiSession);
m_spiSession =
NULL;
return S_OK;
}
. . .
BEGIN_SINK_MAP(CMyPlugin)
SINK_ENTRY_EX(1, __uuidof(DAccEvents), ACCDISPID_BEFOREIMSEND, BeforeImSend)
END_SINK_MAP()
STDMETHOD(BeforeImSend)(IAccSession* session, IAccImSession* imSession, IAccUser* recipient, IAccIm* im)
{
}
private:
CComPtr<IAccSession> m_spiSession;
};
That's all there is to it! If you now rebuild your plugin, your member functions will be called whenever an event occurs in AIMCC. You can then add code in the desired callbacks to process events that you want to handle.
Next Lesson | Back to Table of Contents
Questions? Visit
http://developer.aim.com/
Last updated:
03/17/2007