Connect a managed event source to an unmanaged event stream

I am trying to write a managed library in C # that will act as an event source for an existing C ++ event receiver. The problem I am facing is that when an unmanaged application calls AtlAdvise to get a connection point map for my library, the error message "0x80040200" (CONNECT_E_NOCONNECTION) appears. There are several MSDN articles about this error related to unmanaged ↔ uncontrolled communication and an incorrect map of connection points on the COM server, but nothing about the managed COM server.

I downloaded idl from an unmanaged C ++ server that works, and got a list of events that were fired, and then created the same events in my code by doing the following:

Created by a dll that contains the interfaces that I have to implement

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIDispatch), Guid("xxxxxx")] public interface IMyInterface { ... methods here ... 

Created a dll that has a class that implements 3 required interfaces

 [Guid("xxxxxxx2")] [ComSourceInterfaces(typeof(IMyInterface), typeof(IMyOtherInterface), typeof(IMyThirdInterface))] public class DeviceTranslator : IDisposable, IMyInterface, IMyOtherInterface, IMyThirdInterface { 

delegates created in the managed interface namespace

 namespace myNS.Interfaces { public delegate void DistributeMessage([In] ref OLDMESSAGE Message); 

and created events in real IMyInterface

 event DistributeMessage myDistributeMessage; 

in my class, I implemented an event: public event DistributeMessage myDistributeMessage;

I can place a breakpoint in my constructor, see that the DLL loads properly, but when this part of the unmanaged code gets called, I get the above error: (pDistributeSink is casting CEventSink to IUnknown * and the GUID for IID_IDistributeEvent is the same GUID as IMyInterface)

 hr = AtlAdvise(m_pUnknown, pDistributeSink, IID_IDistributeEvent, &m_dwConnectionPointCookie); 

At this moment, I am scratching my head completely and don’t understand what else AtlAdvise needs to get the connection point map back from the CLR ...

+4
source share
2 answers

So, I waited 5 days, spinning my wheels every day, unable to figure it out before throwing it here, and now in an hour I realized this ... go figure.

Here is what I had to do (thanks codeproject )

I inherited from the outbound event interface (IMyInterface) and defined it in my ClassInterface attribute. I also created delegates in the IMyInterface namespace, and not in the namespace in which the DeviceTranslator class is located.

Turns out I need:

  • ONLY define this in ClassInterface, and then there is no override of the methods in this interface.

  • Creating delegates in my class is the library namespace corresponding to the signature of the IMyInterface method exactly (which correspond to the sink client COM event).

  • create events inside the DeviceTranslator class, which are named exactly like IMyInterface Methods

This is explained very well in the link I wrote above, and was the first place that made him really immerse himself in how everything works and what should be created where.

+4
source

I believe this is the correct link to codeproject: http://www.codeproject.com/KB/COM/cominterop.aspx#NetEvents

+3
source

All Articles