A call to PInvoke function '[…]' has unbalanced the stack

后端 未结 5 1179
失恋的感觉
失恋的感觉 2020-11-27 06:03

I\'m getting this weird error on some stuff I\'ve been using for quite a while. It may be a new thing in Visual Studio 2010 but I\'m not sure.
I\'m trying to call a unam

5条回答
  •  野性不改
    2020-11-27 06:18

    It could also be that in the .NET Framework version 3.5, the pInvokeStackImbalance MDA is disabled by default. Under 4.0 (or maybe VS2010) it is enabled by default.

    Yes. Technically, the code was always wrong, and previous versions of the framework silently corrected it.

    To quote the .NET Framework 4 Migration Issues document: "To improve performance in interoperability with unmanaged code, incorrect calling conventions in a platform invoke now cause the application to fail. In previous versions, the marshaling layer resolved these errors up the stack... If you have binaries that cannot be updated, you can include the element in your application's configuration file to enable calling errors to be resolved up the stack as in earlier versions. However, this may affect the performance of your application."

    An easy way to fix this is to specify the calling convention and make sure it is the same as in the DLL. A __declspec(dllexport) should yield a cdecl format.

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

提交回复
热议问题