Click here to Skip to main content
15,942,847 members
Articles / Programming Languages / C++/CLI

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

Rate me:
Please Sign up or sign in to vote.
4.91/5 (3 votes)
15 Nov 2011Ms-PL2 min read 31.1K   406   14   7
How to implement COM connection point callback for a C++\CLI client.


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:


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


Now just compile the solution and your server is ready.

C++/CLI Client

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


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:


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


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:



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


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

Written By
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.

Comments and Discussions

QuestionGood Pin
Jeong, WonYoung(Brad)26-May-14 3:34
professionalJeong, WonYoung(Brad)26-May-14 3:34 
It's good.

but I think that 'Native C++ to COM' is more good.
because Managed C++ and C#/VB is aleady .NET.
COM of Native C++ can be used in any Languages but, Managed C++ cann't be. Smile | :)
Strength and honor

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

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

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