pinvokestackimbalance——我该如何解决或关闭它? [英] pinvokestackimbalance -- how can I fix this or turn it off?

查看:35
本文介绍了pinvokestackimbalance——我该如何解决或关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚从 vs2008 切换到 vs2010.完全相同的解决方案,除了现在每次调用 C++ dll 都会产生pinvokestackimbalance"异常.

I just switched to vs2010 from vs2008. Exact same solution, except now every single call to a C++ dll yields a 'pinvokestackimbalance' exception.

此异常不会在 2008 年触发.我可以完全访问 C++ dll 和调用应用程序.pinvoke 似乎没有任何问题,但是这个问题使得调试其他问题变得不可能;IDE 会不停地告诉我这些事情.

This exception does not get fired in 2008. I have complete access to the C++ dll and to the calling application. There does not appear to be any problem with the pinvoke, but this problem is making debugging other problems impossible; the IDE is stopping constantly to tell me about these things.

例如,这里是 C# 签名:

For instance, here's the C# signature:

    [DllImport("ImageOperations.dll")]
    static extern void FasterFunction(
        [MarshalAs(UnmanagedType.LPArray)]ushort[] inImage, //IntPtr inImage, 
        [MarshalAs(UnmanagedType.LPArray)]byte[] outImage, //IntPtr outImage, 
        int inTotalSize, int inWindow, int inLevel);

以下是 C++ 方面的样子:

Here's what it looks like on the C++ side:

#ifdef OPERATIONS_EXPORTS
#define OPERATIONS_API __declspec(dllexport)
#else
#define OPERATIONS_API __declspec(dllimport)
#endif
extern "C" {


OPERATIONS_API void __cdecl FasterFunction(unsigned short* inArray, 
                                       unsigned char* outRemappedImage,
                                       int inTotalSize, 
                                       int inWindow, int inLevel);

}

vs2010 和 vs2008 有什么不同会导致这些异常被抛出?我应该向 DllImport 指令添加一组不同的参数吗?

What's different between vs2010 and vs2008 that would cause these exceptions to get thrown? Should I be adding a different set of parameters to the DllImport directive?

推荐答案

首先,要明白代码是错误的(而且一直都是).pInvokeStackImbalance"本身并不是一个例外,而是一个托管调试助手.VS2008默认是关闭的,但是很多人没有开启,所以在VS2010默认是开启的.MDA 不在发布模式下运行,因此如果您为发布而构建,它不会触发.

First, understand that the code is wrong (and always has been). The "pInvokeStackImbalance" is not an exception per se, but a managed debugging assistant. It was off by default in VS2008, but a lot of people did not turn it on, so it's on by default in VS2010. The MDA does not run in Release mode, so it won't trigger if you build for release.

在您的情况下,调用约定不正确.DllImport 默认为 CallingConvention.WinApi,它与 x86 桌面代码的 CallingConvention.StdCall 相同.应该是CallingConvention.Cdecl.

In your case, the calling convention is incorrect. DllImport defaults to CallingConvention.WinApi, which is identical to CallingConvention.StdCall for x86 desktop code. It should be CallingConvention.Cdecl.

这可以通过将行 [DllImport("ImageOperations.dll")] 编辑为:

This can be done by editing the line [DllImport("ImageOperations.dll")] to be:

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

有关详细信息,请参阅 此 MSDN 参考

For more information, see this MSDN reference

这篇关于pinvokestackimbalance——我该如何解决或关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆