以下是來自unmanged DLL功能的代碼。它接受函數指針作爲參數,並簡單地返回被調用函數返回的值。我想從C++非託管代碼調用C#委託。一個無參數的委託工作正常,但與參數的代表崩潰我的程序
extern __declspec(dllexport) int _stdcall callDelegate(int (*pt2Func)());
extern __declspec(dllexport) int _stdcall callDelegate(int (*pt2Func)())
{
int r = pt2Func();
return r;
}
在託管的C#代碼中,我使用委託調用上面的umanged函數。
unsafe public delegate int mydelegate();
unsafe public int delFunc()
{
return 12;
}
mydelegate d = new mydelegate(delFunc);
int re = callDelegate(d);
[DllImport("cmxConnect.dll")]
private unsafe static extern int callDelegate([MarshalAs(UnmanagedType.FunctionPtr)] mydelegate d);
這一切都很好!但如果我想讓我的函數指針/委託採取參數,它會使程序崩潰。 所以,如果我修改代碼如下我的程序崩潰。
改性非託管C++ -
extern __declspec(dllexport) int _stdcall callDelegate(int (*pt2Func)(int));
extern __declspec(dllexport) int _stdcall callDelegate(int (*pt2Func)(int))
{
int r = pt2Func(7);
return r;
}
改性C#代碼 -
unsafe public delegate int mydelegate(int t);
unsafe public int delFunc(int t)
{
return 12;
}
mydelegate d = new mydelegate(delFunc);
int re = callDelegate(d);