加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#包装器的c dll; “运行时检查失败#0 – ESP的值未在函数调用中

发布时间:2020-12-15 19:35:15 所属栏目:百科 来源:网络整理
导读:这是C dll中的代码: 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);publi
这是C dll中的代码:

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 b);

public static void Main(string[] args)
{
  int result = testDelegate(AddIntegers,4,5);
  Console.WriteLine("Code returned:" + result.ToString());
}

当我启动这个小应用程序时,我从这篇文章的标题中得到消息.有人可以帮帮忙吗?

提前致谢,

d

解决方法

Adam是正确的,你在32位版本的Windows上的调用约定上有不匹配.函数指针默认为__cdecl,委托声明默认为CallingConvention.StdCall.当委托调用返回时,不匹配会导致堆栈指针无法正确恢复,从而在C/C++代码的调试版本中触发诊断.

要在C/C++方面修复它:

typedef int (__stdcall * Callback)(int,int);

extern "C" _declspec(dllexport) int testDelegate(Callback addFunction,b);
    return res;
}

要在C#端修复它:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate int AddIntegersDelegate(int number1,int number2);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读