Title ATL and Connection Points
Author Copyright ©1999, Andre v.d. Merwe dart@pobox.com
Date Feb 19 1999
Downloads C++ example 15
  Back to the Zen Home Page

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.


Create a COM DLL

  1. Start a new project - ATL COM AppWizard
  2. Use defaults - click finish

Need a COM object

  1. Add ATL object (on Insert menu/ ClassWizard)
  2. Select "Simple Object"
  3. Name object
  4. Do not modify attributes - (ie leave "Support Connection Points" unchecked!)

Add an interface for the events

  1. open the ".idl" file - FileView in "Source Files" folder
  2. Modify to add events, insert the text indicated by the numbers 1 and 2 on the images
  3. 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

  1. Right click on the event interface (ClassView) and select "Add Method"
  2. Type "OnTest" in the "Method Name" edit box
  3. Click ok - the interface now has a method, the event

Compile the project

  1. 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

  1. Right click the COM object (class view) and select "Implement Connection Point"
  2. Check the event interface box
  3. 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).

  4. Repeat this evertime you add new events, this will ensure that the Fire_XXX methods are generated

Add a method to the COM object

  1. Right click on the object interface
  2. Add a method called "DoTest"

Make the DoTest method fire the event

  1. Double click the "DoTest" method (on the CConnectionTst class, not the interface)
  2. Add a messagebox so that you will know when the method is called
  3. Add a call to "Fire_OnTest()" wich will call the event handler, if the client application is connected

Build the project.

  1. The COM DLL (source) is ready.

The Test Application

Create a new "MFC AppWizard(exe)" project

  1. Page 1 - Select "dialog based"
  2. Page 2 - Use defaults, select "Finished"

Setup dialog

  1. Drop a button onto the dialog box
  2. Double click the button to create the OnClick handler

Interface definition includes

  1. Right click the "Source Files" (FileView)" folder
  2. Select "Add File to Folder"
  3. Select the "ConnectDemo_i.c" file from the COM DLL project This file contains the CLSIDs
  4. Right click this file in FileView
  5. Select the "Not using precompiled headers" If you dont do this VC++ will not compile this file
  6. 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

  1. There may be a more elegant way to do this, but this works...
  2. Select "New ATL Object" from the Insert menu
  3. Click "yes" when asked if you would like to add ATL support
  4. Click cancel - dont add an ATL object

Add a COM class which will connect to the DLL

  1. inherit from IDispatchImpl
  2. param 1 - IConnectionTstEvents - Events interface
  3. param 2 - &IID_IConnectionTstEvents - Events interface CLSID
  4. param 3 - &CLSID_ConnectionTst - CLSID of DLL in which events reside
  5. inherit from CComObjectRoot
  6. Add constructor
    Add entry for IDispatch and IConnectionTstEvents
  8. Add all methods from event interface, in this case only OnTest
  9. Define action for event handler

OnButton1 code

  1. Call CoInitialize
  2. Init the global CComModule variable (_Module)
  3. Create an instance of the CTstConnect class
  4. Call CreateInstance to return a pointer to the IID_IConnectionTst interface
  5. Create an instance of the client object (CTstConnect)
  6. Call DoTest - test what happens when not connected
  7. Call AtlAdvise to connect to server
  8. Call DoTest - event will now be fired
  9. Call AltUnAdvise - disconnect
  10. Call DoTest - no event will be fired
  11. Release server object
  12. 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.