title: Handling a VB6 event in C# - why does it only work sometimes?
body: <p>I have a VB6 application implemented as an ActiveX exe. 
  I also have a C# application that interacts with the VB6 app via COM.</p>
  
  <p>This all works fine except in one scenario.</p>
  
  <p>If the VB6 app is kicked off from the C# app, everything is fine. If, however, the VB6 app is already running stand-alone, then although the COM interface still works, the C# event handlers never fire. </p>
  
  <p>A (very simplified) extract of the code follows, names &amp; GUIDs changed to protect the innocent.</p>
  
  <p>VB6 app: myVB6App.cls (GlobalSingleUse)</p>
  
  <pre><code>Event myEvent()
  
  public function RaiseMyEvent()
      RaiseEvent myEvent
  end function
  </code></pre>
  
  <p>(partial) IDL generated from the built VB6 exe:</p>
  
  <pre><code>...
  [
    uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),
    version(1.10),
    appobject
  ]
  coclass myApp {
      [default] interface _myApp;
      [default, source] dispinterface __myApp;
  };
  ...
  [
    uuid(yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy),
    version(1.10),
    hidden,
    nonextensible
  ]
  dispinterface __myApp {
      properties:
      methods:
          ...
          [id(0x00000005)]
          void myEvent();
          ...
  };
  ...
  </code></pre>
  
  <p>C# app:</p>
  
  <pre><code>public class myAppInterface : IDisposable, ImyAppEvents
  {
      public delegate void myEventDelegate();
      public event myEventDelegate myEventHandler;
      private object _myApp = null;
      private IConnectionPoint _connectionPoint;
      private int _sinkCookie;
  
      public myAppInterface()
      {
          _myApp = Activator.CreateInstance(Type.GetTypeFromProgID("myVB6ProjectName.myApp"));
          Guid g = new Guid("yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy");
          connectionPointContainer.FindConnectionPoint(ref g, out _connectionPoint);
          _connectionPoint.Advise(this, out _sinkCookie);
      }
      public void myEvent()
      {
          if (myEventHandler != null)
          {
              myEventHandler();
          }
      }
  }
  
  [ComImport, Guid("yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"), TypeLibType((short)4240)]
  [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
  public interface ImyAppEvents
  {
      [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(5)]
          void myEvent();
  }
  
  public class myC#App
  {
      private static myAppInterface _vb6App;
      public static myAppInterface VB6Application
      {
          get
          {
              if (_vb6App == null)
              {
                  _vb6App = new myAppInterface();
                  _vb6App.myEventHandler += new myAppInterface.myEventDelegate(DoSomething);
              }
              return _vb6App;
          }
      }
      static void DoSomething()
      {
          //code to actually handle the event
      }
  }
  </code></pre>
  
  <p>As I say, if at the point that <code>Activator.CreateInstance</code> runs, the VB6 exe is not currently running, everything work as expected and the code in <code>DoSomething()</code> is executed when <code>myEvent</code> is fired in the VB6 app. </p>
  
  <p>If the VB6 app is running stand-alone beforehand, the C# app can still control it via COM (methods not shown above for clarity) but the <code>DoSomething()</code> code never runs in response to <code>myEvent</code>.</p>
  
  <p>Any ideas where I'm going wrong?</p>