Exposing events from .NET to COM

I recently ran into problems when exposing events from .NET to COM.

I worked well with this example (conceptually taken from http://blogs.msdn.com/andreww/archive/2008/10/13/exposing-events-from-managed-add-in-objects.aspx ):

// Delegate type for our custom event.

[ComVisible(false)]
public delegate void SomeEventHandler(object sender, EventArgs e);

// Outgoing (source/event) interface.
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IAddInEvents
{
    [DispId(1)]
    void SomeEvent(object sender, EventArgs e);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IAddInEvents))]
public class AddInUtilities :
{       
    // Event field. This is what a COM client will hook up
    // their sink to.
    public event SomeEventHandler SomeEvent;

    inernal void FireEvent(object sender, EventArgs e)
    {
        if (SomeEvent != null)
        {
            SomeEvent(sender, e);
        }
    }
}

      

This works great because the IAddInEvents interface is defined as IDispatch . However, I need to post the event source interface, which is IUnknown . I have no control over the event interface as it comes from a third party library (which will also be a consumer of the published events). Whenever I try to connect to events, the VB environment (where I am trying to sink events) crashes, as does the VBA environment that the third party product (ESRI ArcMap) uses.

I was able to (partially) implement the IConnectionPointContainer interface (which COM uses in the background to handle events) manually, then I can flood the event and go to my IConnectionPointContainer implementation. However, this seems like overkill to me, and I think there should be hidden support in .NET. Secondly, with this approach, I immediately lose the support of delegates.

Does anyone have any experience? Thanks in advance.

+1


source to share


2 answers


Ok, so I was able to implement this by implementing the classic COM IConnectionPointCointainer, IConnectionPoint and IConnection (plus enumeration interfaces). It doesn't integrate into the .NET delegate / event model, but it works.



0


source


Quite simply, you cannot do this. Classic VB does not support non-automatic COM (as you saw).



You will need a wrapper in which you can pass an Automation instance to which the event will be published without automation. You really need to have two different types to handle two separate clients for events (automation and non-automation enabled).

+2


source







All Articles