Click here to Skip to main content
Licence CPOL
First Posted 22 Sep 2006
Views 30,143
Bookmarked 27 times

A practical approach to Connection Point implementation

By | 25 Sep 2006 | Article
This article shows how to implement connection points practically.

Introduction

I have tried to make this tutorial very much practical oriented, and it shows how to implement connection points practically, rather than concentrating on the theory. And before starting anything, I want to give a sincere thanks from the depth of my soul to Mr. Alex C. Punnen for his article published in The Code Project: COM Connection Points.

As this is my first article, I am sincerely thankful for all your suggestions and comments.

Let’s Start

Create a console based application with MFC support. Let’s name it ConClient.

Add another ATL project as the dependency of the current project. Name the ATL project as ConServer.

Create an interface named XMath in to our new ConServer.

But this time, please check the ConnectionPoint check box.

Then, build our ConServer.

You will see a new interface _IXMathEvents in your class view.

Add a new method to our Interface, XMath (please note here that there is no return type or [out, retval] specified).

Add a new method to our event interface (_IXMathEvents in our case).

The method should be like this:

After adding the method to the event interface, please right click on the CoClass of our project, as shown in the picture below:

Then as it is highlighted, in the Implement Connection Point dialog, select it. You are going to see the following screen:

As in the picture, please check the box that says _IXMathEvents. After that, you will see some class generated for us by the wizard named CProxy_IXMathEvents<class T>.

Again, if you implement the connection point, then you will see some methods, like Fire_ExecutionOver inside the CProxy_IXMathEvents class, if you are not seeing the Fire_ExecutionOver method in the CProxy… class.

Double click on the Add(int n1, int n2) function of the CXMath->IXMath->Add function. And write the following line of code:

Fire_ExecutionOver(n1 + n2);

Build the server DLL, in the File view, by right clicking on it.

If you are getting some errors like in the picture below:

Please change the CONNECTION_POINT_ENTRY(IID__IXMathEvents) line to CONNECTION_POINT_ENTRY(DIID__IXMathEvents).

Make the ConClient as the active project now. And add a new class CSink derived from CCmdTarget, and please select the Automation radio button from the Automation panel.

Enter the following line to the StdAfx.h of the client:

#import "ConServer\Debug\ConServer.DLL" named_guids
using namespace CONSERVERLib;

Add a member function to the CSink class, as HRESULT Add(int nResult).

Open the Sink.CPP file and modify the following lines. Comment the INTERFACE_PART(CSink, IID_ISink, Dispatch) line and add INTERFACE_PART(CSink, DIID__IXMathEvents, Dispatch).

BEGIN_INTERFACE_MAP(CSink, CCmdTarget)//DIID__IXMathEvents
    //INTERFACE_PART(CSink, IID_ISink, Dispatch)
    INTERFACE_PART(CSink, DIID__IXMathEvents, Dispatch)
END_INTERFACE_MAP()

In the DISPATCH MAP, add the following line:

DISP_FUNCTION(CSink, "Add", Add, VT_I4, VTS_I4)

The changes have been marked with the red lines in Sink.CPP.

Add #include “Sink.h” to your main function’s file. And write the following lines of code to your main function:

IXMathPtr ptrMath;
CoInitialize(NULL);
ptrMath.CreateInstance(__uuidof(XMath));

if(ptrMath)
{
    IConnectionPointContainerPtr ptrConPntCnt = (IDispatch*)ptrMath;
    IConnectionPointPtr ptrCon;
    IUnknown* pHandler = NULL;
    DWORD dwCookie;

    if(ptrConPntCnt != NULL)
    {
        ptrConPntCnt->FindConnectionPoint(DIID__IXMathEvents,&ptrCon);
                    
        CSink *pSink = new CSink;

        pHandler = pSink->GetInterface(&IID_IUnknown);
        ptrCon->Advise(pHandler,&dwCookie);
                
        ptrMath->Add(10, 20);

        ptrCon->Unadvise(dwCookie);

        ptrCon.Release();
        ptrConPntCnt.Release();
        delete pSink;

    }
    ptrMath.Release();
}
CoUninitialize();

After successfully running this program, you will see a message box showing you the value as 30 as in the figure down below:

In my second version of the article, I am intending to implement connection points using eVC++ 4.0 and VS 7.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Satyam Kumar

Software Developer

India India

Member

Hi All,
 
Currently i am working as a role Application Engineer. I am working on vehicle protocols like CAN, SCP, ISO & 14229.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood work Pinmembernigulavy0:06 12 Nov '08  
GeneralGenerate event in thread... Pinmemberachainard23:29 7 Nov '07  
Generalcannot instantiate abstract class Pinmemberhdtntran19:17 2 May '07  
GeneralRe: cannot instantiate abstract class PinmemberSatyam Kumar15:21 12 May '07  
GeneralCut 'n' paste Pinmembernorm .net9:28 22 Sep '06  
GeneralRe: Cut 'n' paste PinmemberSatyam Kumar22:02 24 Sep '06  
GeneralRe: Cut 'n' paste PinmemberJigar M9:42 11 Feb '07  
I found this article very useful.. its really good for beginner.. to clear their concepts and a time saver!!
 
Jigar

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 Sep 2006
Article Copyright 2006 by Satyam Kumar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid