Introduction:
This article shows how to implement connection points using ATL. This can be done automatically using the
AppWizard etc. I chose not to use this method for two reasons. Firstly there seems to be a bug with
VC++ 6.0 (I assume its fixed in the service pack), and secondly I wanted to see how it was done.
This is not a full "article" but rather a step-by-step guide to show you how its done.
The COM DLL
Create a COM DLL
- Start a new project - ATL COM AppWizard
- Use defaults - click finish
Need a COM object
- Add ATL object (on Insert menu/ ClassWizard)
- Select "Simple Object"
- Name object
- Do not modify attributes - (ie leave "Support Connection Points" unchecked!)
Add an interface for the events
- open the ".idl" file - FileView in "Source Files" folder
- Modify to add events, insert the text indicated by the numbers 1 and 2 on the images
- Add new CLSID (generated using uuidgen or whatever)
The events interface should now be displayed on the ClassView tab,
this means that you can add methds to this interface like any other
interface created by ClassView
Add an event
- Right click on the event interface (ClassView) and select "Add Method"
- Type "OnTest" in the "Method Name" edit box
- Click ok - the interface now has a method, the event
Compile the project
- This compiles the MIDL files and creates the type libraries
Always do this before adding support for a event/connection point
Add connection point support to the COM object
- Right click the COM object (class view) and select "Implement Connection Point"
- Check the event interface box
- Click ok
The COM object now supports connection points, specifically the event interface you created.
VC++ will create a class called "CProxyIxxxx" which will define methods to invoke
the events (search for a IConnectionPoint and fire it).
- Repeat this evertime you add new events, this will ensure that the Fire_XXX methods are generated
Add a method to the COM object
- Right click on the object interface
- Add a method called "DoTest"
Make the DoTest method fire the event
- Double click the "DoTest" method (on the CConnectionTst class, not the interface)
- Add a messagebox so that you will know when the method is called
- Add a call to "Fire_OnTest()" wich will call the event handler, if the client
application is connected
Build the project.
- The COM DLL (source) is ready.
The Test Application
Create a new "MFC AppWizard(exe)" project
- Page 1 - Select "dialog based"
- Page 2 - Use defaults, select "Finished"
Setup dialog
- Drop a button onto the dialog box
- Double click the button to create the OnClick handler
Interface definition includes
- Right click the "Source Files" (FileView)" folder
- Select "Add File to Folder"
- Select the "ConnectDemo_i.c" file from the COM DLL project This file contains the CLSIDs
- Right click this file in FileView
- Select the "Not using precompiled headers" If you dont do this VC++ will not compile this file
- Go back to the "ConnectTestDlg.cpp" file (dialog implementation)
Add a #include to the "ConnectDemo.h" file from the COM DLL project
This file defines the interfaces etc
Add ATL support
- There may be a more elegant way to do this, but this works...
- Select "New ATL Object" from the Insert menu
- Click "yes" when asked if you would like to add ATL support
- Click cancel - dont add an ATL object
Add a COM class which will connect to the DLL
- inherit from IDispatchImpl
- param 1 - IConnectionTstEvents - Events interface
- param 2 - &IID_IConnectionTstEvents - Events interface CLSID
- param 3 - &CLSID_ConnectionTst - CLSID of DLL in which events reside
- inherit from CComObjectRoot
- Add constructor
- Add COM_MESSAGE_MAP
Add entry for IDispatch and IConnectionTstEvents
- Add all methods from event interface, in this case only OnTest
- Define action for event handler
OnButton1 code
- Call CoInitialize
- Init the global CComModule variable (_Module)
- Create an instance of the CTstConnect class
- Call CreateInstance to return a pointer to the IID_IConnectionTst interface
- Create an instance of the client object (CTstConnect)
- Call DoTest - test what happens when not connected
- Call AtlAdvise to connect to server
- Call DoTest - event will now be fired
- Call AltUnAdvise - disconnect
- Call DoTest - no event will be fired
- Release server object
- Call CoUninitialize
All information on these www pages is copyright (©) 1997 Andre .v.d. Merwe
And may not be copied or mirrored without my permission.