title: Trigger events generated from a managed class in C++/CLI
body: <p>I am trying to trigger events from a C++ library into an app in C#. Previous question regarding this: <a href="https://stackoverflow.com/questions/50695113/using-unmanaged-pointer-to-callback-function-on-managed-c">Using unmanaged pointer to callback function on managed C++</a> </p>
  
  <p>Thanks to the help I received in that question comments, and this example on a previous question answered by Hans Passant: <a href="https://stackoverflow.com/questions/2972452/c-cli-pass-managed-delegate-to-unmanaged-code">c++/cli pass (managed) delegate to unmanaged code</a> I <em>thought</em> I got It working, but when tested on real hardware, the events do not trigger. From the debugger I can see the function callback_function is called when an event happens triggered by hardware I/O, the instructions there print the corresponding lines, then TriggerEvent(1) is called, but the event is not received in the C# code I did to handle it.</p>
  
  <p>I suspect this has to do with how a new instance of CppDIOHandler is created just before "Step 3", instead of creating the delegate in the original. I tried to get a handle of current object (similar to "this" but with managed code, using GCHandle) to create the CallbackDelegate object, with no luck. I do not understand why I can't simply use callback = gcnew CallbackDelegate(&amp;callback_function) instead of that line.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;WDT_DIO.H&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  using namespace System;
  using namespace System::Runtime::InteropServices;
  
  public ref class PinStateChangedEventArgs : public EventArgs
  {
  public:
      property int Direction;
      property DateTime TimeReached;
  };
  
  public ref class CppDIOHandler
  {
  public:
      CppDIOHandler() {
      }
  
      delegate void CallbackDelegate(COS_INT_CALLBACK_ARG*);
      static CallbackDelegate^ callback;
  
      void __stdcall callback_function(COS_INT_CALLBACK_ARG* arg)
      {
          printf("data=0x%02x, flag=0x%02x, seq=%02d\n",
              arg-&gt;portData, arg-&gt;intrFlag, arg-&gt;intrSeq);
          TriggerEvent(1);
      }
  
  
      static void StartDIO() {
          //Step 1, initialize DIO library by invoking InitDIO()
          if (!InitDIO())
          {
              Console::WriteLine("InitDIO --&gt; FAILED");
              return;
          }
          Console::WriteLine("InitDIO --&gt; PASSED");
  
          //Step 2, setup Change-of-State Interrupt mask and level/edge mode
          COS_INT_SETUP setup;
          memset(&amp;setup, 0, sizeof(setup));
          setup.portMask = 0x0f; // 00001111b, enable ch.0~3
          setup.edgeMode = 0x00; // generate interrupt on level change
          setup.edgeType = 0x00; // rising/falling edge, only effective when edgeMode = 1
  
          if (!SetupDICOS(&amp;setup, sizeof(setup)))
          {
              Console::WriteLine("SetupDICOS --&gt; FAILED");
              return;
          }
          Console::WriteLine("SetupDICOS --&gt; PASSED");
  
          CppDIOHandler^ cdh = gcnew CppDIOHandler();
          callback = gcnew CallbackDelegate(cdh,&amp;callback_function);
          IntPtr stubPointer = Marshal::GetFunctionPointerForDelegate(callback);
          COS_INT_CALLBACK functionPointer = static_cast&lt;COS_INT_CALLBACK&gt;(stubPointer.ToPointer());
          //TO-DO: Make callback static and remove the GC instruction
          //GC::KeepAlive(callback); 
  
          //Step 3, register the callback function
          if (!RegisterCallbackDICOS(functionPointer))
          {
              Console::WriteLine("RegisterCallbackDICOS --&gt; FAILED");
              return;
          }
          Console::WriteLine("RegisterCallbackDICOS --&gt; PASSED");
          //Step 4, start the DI Change-of-State Interrupt
          if (!StartDICOS())
          {
              Console::WriteLine("StartDICOS --&gt; FAILED");
              return;
          }
          Console::WriteLine("StartDICOS --&gt; PASSED");
      }
  
      void StopDIO() {
          //Step 5, stop the DI Change-of-State Interrupt operation
          if (!StopDICOS())
          {
              Console::WriteLine("StopDICOS --&gt; FAILED");
              return;
          }
          Console::WriteLine("StopDICOS --&gt; PASSED");
      }
  
      void TriggerEvent(int direction)
      {
              PinStateChangedEventArgs^ args = gcnew PinStateChangedEventArgs();
              args-&gt;Direction = direction;
              args-&gt;TimeReached = DateTime::Now;
              OnPinStateChanged(args);
      }
  
      event EventHandler&lt;PinStateChangedEventArgs^&gt;^ PinStateChanged;
  
      virtual void OnPinStateChanged(PinStateChangedEventArgs^ e)
      {
          PinStateChanged(this, e);
      }
  };
  </code></pre>
  
  <p>The C# code is trivial, so seems unlikely the problem is there, I just did:</p>
  
  <pre><code>CppDIOHandler cc = new CppDIOHandler();
  CppDIOHandler.StartDIO();
  cc.PinStateChanged += Cs_PinStateChanged;
  </code></pre>
  
  <p>But Cs_PinstateChanged callback is never executed. What Is the proper way to solve this? Is really necessary to use GCHandle to reference current object in managed code or this has nothing to do with my problem?</p>