Click here to Skip to main content
11,640,211 members (63,745 online)
Click here to Skip to main content

COM connection point callback for a C++/CLI client

, 15 Nov 2011 Ms-PL 11.5K 311 14
Rate this:
Please Sign up or sign in to vote.
How to implement COM connection point callback for a C++\CLI client.

Introduction

If you have to write interop code, should you use C# and P/Invoke, or should you use C++/CLI? The clients for a COM connection point server can be written in C++ and in C# managed code. Recently, I got a query about the possibility of a managed C++/CLI client for a COM connection point server. A search on the web gave many samples of C++ and C# managed code but none for a C++/CLI client. It is a topic of debate; refer to this blog. It says “With C++/CLI, you can use the API natively exactly like it's meant to be used, and then "expose" the code to the managed world via C++/CLI”. This motivated me to write an article on how to implement a COM connection point callback for a C++/CLI client.

COM Server with a Connection Point

To start, I needed a COM server with a connection point. In my scenario, the server exposes a COM method:

HRESULT Add(int nFirst, int nSecond)

The server also defines ConnectionPointContainer and connection points so that clients can register with it. In addition, the server defines an interface, _IAddEvents, which has two methods in it:

HRESULT AdditionCompleted(int nResult)
HRESULT AdditionStarted()

You should have a server interface IAdd that can be called from the client. If you build the server, you will notice that there are two interfaces defined here. One is IAdd, which implements IDispatch, and the other is the dispinterface _IAddEvents. The client provides the implementation for _IAddEvents and invokes the Add method on the server. The server fires the AdditionStarted and AdditionCompleted methods on the client to notify it appropriately. Then the client performs the appropriate actions associated with these events.

ATLConnectionPointServer.idl should add methods AdditionStarted and AdditionCompleted to the _IAddEvents interface, as shown in figure:

1.png

Implement the Add method on the server and the trigger points for firing events from the server.

2.png

Now just compile the solution and your server is ready.

C++/CLI Client

Create a new project Visual C++ => CLR => CLR Console Application.

3.png

Import the server DLL to managed code to obtain ATLConnectionPointServerLib.dll by using the Microsoft® .NET Framework Type Library to Assembly Converter tool, tlbimp.exe, running the following command:

tlbimp ATLConnectionPointServer.dll 

Reference the resulting assembly in your managed project, as shown in the figure below:

4.png

Provide an implementation for the sink interface on the client, such as the one shown here:

5.png

You have to register the sink with the server so the server can invoke the sink when firing the events. With the managed client, you register individual methods as delegates with the server. To accomplish this, you create an instance of the sink object:

Create an instance of the server object and add the AdditionStarted and AdditionCompleted event handlers separately, like this:

AddClass^ a = gcnew AddClass();

if(a == nullptr) //check to see if a is still nullptr
	Console::WriteLine("reference not allocated to handle");

ManagedSink^ ms = gcnew ManagedSink();
if(ms == nullptr) //check to see if a is still nullptr
	Console::WriteLine("reference not allocated to handle");

a->AdditionStarted += gcnew _IAddEvents_AdditionStartedEventHandler
	(ms, &ManagedSink::AdditionStarted );
a->AdditionCompleted += gcnew _IAddEvents_AdditionCompletedEventHandler
	(ms, &ManagedSink::AdditionCompleted );

a->Add(1, 5);

a->AdditionStarted -= gcnew _IAddEvents_AdditionStartedEventHandler
	(ms, &ManagedSink::AdditionStarted );;
a->AdditionCompleted -= gcnew _IAddEvents_AdditionCompletedEventHandler
	(ms, &ManagedSink::AdditionCompleted );

Now just compile the solution and your C++/CLI client is ready.

Run the MC++ project. You should see the following output:

6.png

History

  • 15 Nov 2011: Corrected image links.
  • 20 Oct 2011: Initial release.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Mitendra Anand
Architect
India India
My name is Mitendra Anand and my work is focused around application development which includes a lot of prototyping of new solutions.

While I have a background in C++/VC++ programming, my daily work is mostly spent in C++, Sybase, SQL, Unix/Windows.

You may also be interested in...

Comments and Discussions

 
QuestionGood Pin
jwyhydra26-May-14 3:34
memberjwyhydra26-May-14 3:34 
Generalthansk,it is very useful for me Pin
askformore51127-Jun-13 22:35
memberaskformore51127-Jun-13 22:35 
Questionvery googd Pin
askformore51127-Jun-13 22:34
memberaskformore51127-Jun-13 22:34 
QuestionGood article - short and sweet :) Pin
Member 864691016-Feb-12 1:03
memberMember 864691016-Feb-12 1:03 
AnswerRe: Good article - short and sweet :) Pin
Mitendra Anand16-Feb-12 20:16
memberMitendra Anand16-Feb-12 20:16 
QuestionMy vote of 5 Pin
Sharjith16-Nov-11 16:41
memberSharjith16-Nov-11 16:41 
AnswerRe: My vote of 5 Pin
Mitendra Anand16-Nov-11 17:13
memberMitendra Anand16-Nov-11 17:13 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 15 Nov 2011
Article Copyright 2011 by Mitendra Anand
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid