這裏是C++ DLL的代碼:C++ dll的C#包裝器; 「運行時檢查失敗#0 - 在函數調用中未正確保存ESP的值。」錯誤
extern "C" _declspec(dllexport) int testDelegate(int (*addFunction)(int, int), int a, int b)
{
int res = addFunction(a, b);
return res;
}
,這裏是在C#代碼:
public delegate int AddIntegersDelegate(int number1, int number2);
public static int AddIntegers(int a, int b)
{
return a + b;
}
[DllImport("tester.dll", SetLastError = true)]
public static extern int testDelegate(AddIntegersDelegate callBackProc, int a, int b);
public static void Main(string[] args)
{
int result = testDelegate(AddIntegers, 4, 5);
Console.WriteLine("Code returned:" + result.ToString());
}
當我開始這個小應用程序,我從這個帖子的標題得到的消息。有人可以幫忙嗎?
由於提前,
d
啊,upvoting你的答案'CallingConvention.Cdecl',因爲我是完全不知道這一點。 – 2010-03-13 10:36:48
夥計們,非常感謝你。你已經解決了我的問題。 – 2010-03-15 07:21:29
雖然,這適用於我,而不是 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int AddIntegersDelegate(int number1,int number2); 我把: [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int AddIntegersDelegate(int number1,int number2); – 2010-03-15 10:04:04