title: Exposing events from .NET to COM
body: <p>recently I have been encountering problems with exposing events from .NET to COM.</p>
  
  <p>I have been successful with this example (conceptually taken from <a href="http://blogs.msdn.com/andreww/archive/2008/10/13/exposing-events-from-managed-add-in-objects.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/andreww/archive/2008/10/13/exposing-events-from-managed-add-in-objects.aspx</a>):</p>
  
  <p>// The delegate type for our custom event.</p>
  
  <pre><code>[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);
          }
      }
  }
  </code></pre>
  
  <p>This works fine, because the IAddInEvents interface is defined as <strong>IDispatch</strong>. However, I need to <strong>publish an event source interface which is IUnknown</strong>. I do not have control over the event interface as it comes from a third-party library (which will also be the consumer of the published events). Whenever I try to hook to the events, VB environment (where I am trying to sink the events) crashes, so does the VBA environment which the third-party product (ESRI ArcMap) uses.</p>
  
  <p>I have been able to (partially) implement <strong>IConnectionPointContainer</strong> interface (which COM uses in the background to handle events) manually, then I am able to sink the event and step into my IConnectionPointContainer implementation. However, this seems like an overkill to me and I think that there must an implicit support for this in .NET. Second, with this approach I instantly lose the delegate support.</p>
  
  <p>Does anyone have any experience with this?
  Thanks in advance.</p>