.net 对于这个简单的示例,为什么我会收到“检测到 PInvokeStackImbalance”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5602645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:27:42  来源:igfitidea点击:

Why do I get "PInvokeStackImbalance was detected" for this simple example?

.netpinvokeextern

提问by Justin

I'm creating a very simple PInvoke sample:

我正在创建一个非常简单的 PInvoke 示例:

extern "C" __declspec(dllexport) int Add(int a, int b)
{
    return a + b;
}

[DllImport("CommonNativeLib.dll")]
extern public static int Add(int a, int b);

return NativeMethods.Add(a, b);

But whenever I call the above NativeMethods.Addmethod I get the following managed debug assistant:

但是每当我调用上述NativeMethods.Add方法时,我都会得到以下托管调试助手:

PInvokeStackImbalance was detected Message: A call to PInvoke function 'CommonManagedLib!CommonManagedLib.NativeMethods::Add' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

检测到 PInvokeStackImbalance 消息:对 PInvoke 函数“CommonManagedLib!CommonManagedLib.NativeMethods::Add”的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。

The call then completes with the expected return value, but having the MDA message appear is both annoying and worrying - I don't fully understand PInvoke yet, but from what I've read I'm pretty sure that my signature is correct - what am I doing wrong?

然后调用以预期的返回值完成,但是出现 MDA 消息既烦人又令人担忧 - 我还没有完全理解 PInvoke,但从我读过的内容来看,我很确定我的签名是正确的 - 什么我做错了吗?

This is all on a 32-bit OS.

这一切都在 32 位操作系统上。

回答by user541686

You need to instead use either

您需要改用要么

[DllImport("CommonNativeLib.dll", CallingConvention = CallingConvention.Cdecl)]

or

或者

extern "C" __declspec(dllexport) int __stdcall Add(int a, int b) ...

because regular C functions work differently than the Windows API functions; their "calling conventions" are different, meaning how they pass around parameters is different. (This was hinted at in the error.)

因为常规 C 函数的工作方式与 Windows API 函数不同;它们的“调用约定”不同,这意味着它们传递参数的方式不同。(这是在错误中暗示的。)

回答by Vijay Kumbhoje

The Stack Imbalance reasons are either the signature is not matching else Calling Convention by default calling convention is stdcall. When your calling convention is stdcall callee cleans the stack if you want caller to clean the stack us cdecl calling convention. you can find more Here

堆栈不平衡的原因要么签名不匹配,要么调用约定默认调用约定是 stdcall。当您的调用约定是 stdcall 时,如果您希望调用方使用 cdecl 调用约定清理堆栈,则被调用方会清理堆栈。你可以在这里找到更多

But if you are facing because of signature, just go through above link Solve Signature based Stack Imbalance issues using PInvoke extension

但是如果你因为签名而面临,只需通过上面的链接 解决基于签名的堆栈不平衡问题使用 PInvoke 扩展