Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering handlers for .NET COM event in C++

Tags:

c++

c#

interop

com

I've been following the 'tutorials' of how to expose a .NET framework through COM ( http://msdn.microsoft.com/en-us/library/zsfww439.aspx and http://msdn.microsoft.com/en-us/library/bd9cdfyx.aspx ). Everything works except for the events part. When I add events to the C# interface the following C++ code is generated:

struct __declspec(uuid("..."))
_MessageEventHandler : IDispatch
{};

struct __declspec(uuid("..."))
IConnection : IDispatch
{
  virtual HRESULT __stdcall add_MessageEvent (
    /*[in]*/ struct _MessageEventHandler * value ) = 0;
  virtual HRESULT __stdcall remove_MessageEvent (
    /*[in]*/ struct _MessageEventHandler * value ) = 0;
}

The problem is that I haven't found any info on how to use this in C++. Do I need to derive from _MessageEventHandler and implement operator()? Or something else entirely?

(Note that for the moment I'm also trying the more documented approach of using IConnectionPointContainer and IConnectionPoint.)

like image 302
Randy Voet Avatar asked Oct 25 '22 21:10

Randy Voet


1 Answers

It has been a long time since I used COM and at that time I was using Visual C++ 6.0. I remember that implementing sinks for COM connection points was not a straightforward process. There were multiple ways for implementing them, depending if you used MFC or ATL. Maybe there are easier ways now. Here are couple of links that can help you:

Code Project - Sinking events from managed code in unmanaged C++
Code Project - COM - large number of articles about COM
Code Project - Handling COM Events in a Console Application
Code Project - Handling COM Events in a Console Application, Part II

like image 184
Robert Vuković Avatar answered Nov 15 '22 11:11

Robert Vuković