title: Interop C++ to C#, passing callback to c++ functions
body: <p>I am trying to use a native dll using DllImport in C#. Most of the methods are working fine but I am stuck in registering to callbacks. here is the sample:-</p>
  
  <ol>
  <li>The function used for callback is <code>static int SetReceiver(IMessageReceiver* pReceiver);</code></li>
  <li><p>Definition for IMessageReceiver is </p>
  
  <pre><code>class IMessageReceiver
  {
  public:
     virtual void OnMessage(unsigned char ucCommand, const void* pData, size_t size) = 0;
  };
  </code></pre></li>
  <li><p>In C++ it is done like this:</p>
  
  <pre><code>class A : IMessageReceiver{ implementation of virtual function}
  SetReceiver(this); // this is object of Class A.
  </code></pre></li>
  <li><p>I want to achieve same in C#. What i did is created one Interface IMessageReceiver and implemented in class. I am creating the object of that class and passing it as parameter but getting exception.</p>
  
  <pre><code>[DllImport("Native.dll", CallingConvention = CallingConvention.Cdecl)]
  public static extern void SetMessageReceiver(IMessageReceiver pReceiver);
  
  SetMessageReceiver(pReceiver); 
  </code></pre></li>
  </ol>