Introduction
This is basically to explain how to use an automation out of proc server. So I used MSN Messenger. My article explains a few things about MSN Messenger. It has one ATL class CIMSGEvents
which handles the events fired by MSN Messenger. It catches events fired by MSN Messenger and just outputs about it in an EditBox.
Steps to use MSN Messenger
- Create a default MFC project (dialog based without using automation option).
- Open ClassWizard select automation tab and select the dlg class and click on add class from type library through type library of Messenger i.e. c:\program files\messenger\msmsgs.exe. Select from the list box the following classes:
IMessengerApp3
IMsgrObject2
DMsgrObjectEvents
(Not very important but insert it to see the dispid for every event. You can also use OLE View to see them)
- Insert a new ATL object and name it something like
IMSGEvents
.
Into the Code
Both IMessengerApp3
and IMsgrObject2
are default interfaces of their CoClasses. Check it in OLE View. IMessengerApp3
dispinterface can be used for controlling the MSN app so first to deal with it. If you see it in generated code you will find it is derived from COleDispatchDriver
. So now we need to call CoCreateInstance
to initialize the MSN Messenger App like this:
HRESULT hres=CoCreateInstance(__uuidof(CLSID_MessengerApp),
NULL,CLSCTX_LOCAL_SERVER,
__uuidof(IID_IMessengerApp3),
(void**)&m_MsnApp);
if(FAILED(hres))AfxMessageBox("Cannot start msn app");
Now to the second dispinterface IMsgrObject2
. This is a bit more important. We have to initialize it too so again use CoCreateInstance
:
HRESULT hres=CoCreateInstance(__uuidof(CLSID_MsgrObject),
NULL,CLSCTX_LOCAL_SERVER,
__uuidof(IID_IMsgrObject2),(void**)&m_MsgObject);
if(FAILED(hres))AfxMessageBox("Cannot get msgr Object");
The third task, a little more complicated, is to make a connection and advise the Messenger. We have to find the IID_DMsgrObjectEvents
interface and get event notification. I used the following code:
IConnectionPoint* spCPC;
IUnknown* mpUnk;
IConnectionPointContainer* punkICPC;
mpUnk=m_MsgObject;
HRESULT hres=mpUnk->QueryInterface(IID_IConnectionPointContainer,
(void**)&punkICPC);
if(FAILED(hres))AfxMessageBox("Cannot get IConnectionPoint interface");
hres=punkICPC->FindConnectionPoint(
__uuidof(IID_DMsgrObjectEvents),&spCPC);
if(FAILED(hres))
AfxMessageBox("Cannot get IID_DMsgrObjectEvents interface");
hres=spCPC->Advise(pMsgEvents,&m_dwAdviseCookie);
if(FAILED(hres))AfxMessageBox("Failed to advise");
Now move to CIMSGEvents
and override the Invoke
method so to see what event is going on. Declare this in the header and source file:
STDMETHOD(Invoke)(DISPID, REFIID, LCID, WORD, DISPPARAMS*,
VARIANT*, EXCEPINFO*, UINT*);
HRESULT CIMSGEvents::Invoke(DISPID dispidMember,
REFIID riid, LCID lcid, WORD wFlags,
DISPPARAMS* pDispParams, VARIANT* pvarResult,
EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
return S_OK;
}
Now to see which event is being fired you can check the dispidMember
. See the source for more info. It is very important to note that the parameters you see listed in OLE View are accessed in reverse. So to access the last parameter you have to use code like this
pDispParams->rgvarg[0]
and to access first parameter you have to use the number which is the maximum number of parameters, like for 4th param you have to use pDispParams->rgvarg[3]
. So I tried to catch all the events and then output it in an EditBox just to see what happens when the user performs certain actions. It works like a spy.
Thanks and Forgive me
I would like to say thanks to Michael Dunn for his cool articles on COM otherwise I wouldn't have started using COM otherwise, and thanks to Mumtaz Zaheer for always helping me. I have been trying to program since I was sixteen and now I am precisely eighteen. I have been programming COM for past 2 months so you see it's a long period. Well I have submitted this article to ask you people if I have done it the right way or not. So forgive me if I have done anything wrong because I am a beginner. I am basically a student of Chartered Accountancy in Pakistan so I also need some comments about which course line for programming I concentrate more on, like MTS which would relate to my Accountancy Skills. Please give comments.