Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A practical approach to Connection Point implementation

0.00/5 (No votes)
25 Sep 2006 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here