title: C# Pinvoke with C struct contains function pointer
body: <p>I struggled this for a while, still not able to figured out how to write code in C# side</p>
  
  <p>C++ DLL</p>
  
  <pre class="lang-cpp prettyprint-override"><code>typedef void (WINAPI *P_HelloWorld)(void);
  typedef struct {
  
  P_HelloWorld pHelloWorld;
  
  }FUNC_PARAM;
  
  void Func4(FUNC_PARAM* pFunc)
  {
     pFunc.pHelloWorld();
  }
  </code></pre>
  
  <p>C# Side:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public delegate void P_HelloWord();
  [StructLayout(LayoutKind.Sequential)]
  public struct FUNC_PARAM
   {
     public P_HelloWord pHelloWorld;
   }
  
    [DllImport("EMV_DLL.dll")]
  
    public extern static void Func4(FUNC_PARAM[] pFunc);
  
   void main()
   {
     FUNC_PARAM g;
     g.pHelloWorld = new P_HelloWord(this.myHelloWorld);
     Func4(new FUNC_PARAM[] { g });
    }
  
  void myHelloWorld()
  {
     MessageBox.Show("My Hello World");
  }
  </code></pre>
  
  <p>The above C# code doesn't work, when execute Func4 function, it throws out of memory exception.</p>
  
  <p>Anybody could help me?</p>