Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#C+的包装+;动态链接库&引用;运行时检查失败“0-函数调用中未正确保存ESP的值。”;错误 这里是C++中的代码: extern "C" _declspec(dllexport) int testDelegate(int (*addFunction)(int, int), int a, int b) { int res = addFunction(a, b); return res; }_C#_C++_Dll_Callback - Fatal编程技术网

C#C+的包装+;动态链接库&引用;运行时检查失败“0-函数调用中未正确保存ESP的值。”;错误 这里是C++中的代码: extern "C" _declspec(dllexport) int testDelegate(int (*addFunction)(int, int), int a, int b) { int res = addFunction(a, b); return res; }

C#C+的包装+;动态链接库&引用;运行时检查失败“0-函数调用中未正确保存ESP的值。”;错误 这里是C++中的代码: extern "C" _declspec(dllexport) int testDelegate(int (*addFunction)(int, int), int a, int b) { int res = addFunction(a, b); return res; },c#,c++,dll,callback,C#,C++,Dll,Callback,下面是C#中的代码: 当我启动这个小应用程序时,我会从这篇文章的标题中得到消息。有人能帮忙吗 提前感谢, D它通常意味着“接口不匹配”:用于编译客户端的声明与dll的实际版本不同。testDelegate的函数指针参数需要标记为\uu stdcall,否则调用它将损坏堆栈(因为它使用不同的调用约定。)Adam是正确的,您在32位版本的Windows上的调用约定不匹配。函数指针默认为_cdecl,委托声明默认为CallingConvention.StdCall。不匹配导致在委托调用返回时堆栈指针无

下面是C#中的代码:

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

提前感谢,


D

它通常意味着“接口不匹配”:用于编译客户端的声明与dll的实际版本不同。

testDelegate的函数指针参数需要标记为
\uu stdcall
,否则调用它将损坏堆栈(因为它使用不同的调用约定。)Adam是正确的,您在32位版本的Windows上的调用约定不匹配。函数指针默认为_cdecl,委托声明默认为CallingConvention.StdCall。不匹配导致在委托调用返回时堆栈指针无法正确还原,从而触发调试中的诊断构建您的C/C++代码

要在C/C++端修复它,请执行以下操作:

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

extern "C" _declspec(dllexport) int testDelegate(Callback addFunction, int a, int b)
{
    int res = addFunction(a, b);
    return res;
}
要将其固定在C#侧:


啊,对
调用Convention.Cdecl
的答案进行表决,因为我完全没有意识到这一点。不过,当[UnmanagedFunctionPointer(CallingConvention.Cdecl)]公共委托int AddIntegersDelegate(int number1,int number2)时,这对我是有效的;我把[UnmanagedFunctionPointer(CallingConvention.StdCall)]公共委托int AddIntegersDelegate(int number1,int number2);但是,您保存了日期。:)
typedef int (__stdcall * Callback)(int, int);

extern "C" _declspec(dllexport) int testDelegate(Callback addFunction, int a, int b)
{
    int res = addFunction(a, b);
    return res;
}
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate int AddIntegersDelegate(int number1, int number2);