A practical approach to Connection Point implementation

本文通过构建基于MFC的控制台应用和依赖的ATL项目,详细演示了如何在ATL项目中实现COM连接点,并通过客户端应用进行调用,确保实现过程的实践性和易懂性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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, likeFire_ExecutionOver inside the CProxy_IXMathEvents class, if you are not seeing theFire_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 toCONNECTION_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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值