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:
(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
Into the Code
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:
(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
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:
if(FAILED(hres))AfxMessageBox("Cannot get IConnectionPoint interface");
AfxMessageBox("Cannot get IID_DMsgrObjectEvents interface");
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)
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
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. 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.