当前位置:编程学习 > C#/ASP.NET >>

C# PInvoke with struct contains function pointer

I struggled this for a while, still not able to figured out how to write code in C# side
 
C++ DLL 

typedef void (WINAPI *P_HelloWorld)(void);

typedef struct {

P_HelloWorld pHelloWorld;

}FUNC_PARAM;

void Func4(FUNC_PARAM* pFunc)
{
pFunc.pHelloWorld();
}

C# Side:
 public delegate void P_HelloWord();

[StructLayout(LayoutKind.Sequential)]

 public struct FUNC_PARAM
 {`enter code here`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");
 }


The above C# code doesn't work, when execute Func4 function, it throws out of memory exception.

Anybody could help me?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,