Pinvokestackimbalance - how can I fix or disable this?

I just switched to vs2010 from vs2008. The exact same solution, except that every time every call in C ++ dll gives a pinvokestackimbalance exception.

This exception does not start in 2008. I have full access to the C ++ dll and the calling application. There are no problems with pinvoke, but this problem makes it impossible to debug other problems; The IDE is constantly stopping to tell me about these things.

For example, here is 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); 

Here's how it looks 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); } 

What is the difference between vs2010 and vs2008, which can cause the throwing of these exceptions? Should I add another set of parameters to the DllImport directive?

+56
c ++ c # visual-studio visual-studio-2010 pinvoke
Aug 17 '10 at 20:45
source share
5 answers

First, understand that the code is incorrect (and always has been). "PInvokeStackImbalance" is not an exception per se, but a managed debugging assistant. It was disabled by default in VS2008, but many people did not enable it, so it is enabled by default in VS2010. MDA does not start in Release mode, so it will not start if you create it for release.

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

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

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

See this link on MSDN for more information.

+96
Aug 17 2018-10-18T00:
source share

To disable it:

  • CTRL + ALT + E
  • In the Managed Debugging Assistants section, clear the PInvokeStackImbalance check box.
+33
Mar 12 2018-12-12T00:
source share

It’s better to solve this problem, it’s not so difficult here, I mentioned some of the methods, it can be just like some of my friends mentioned above. I work with the PCSC Smartcard application, which I spend about one week, is angry, many changes finally got solutions.

For me, his work with the PInvoke Extension, which I installed for VS2010, you can download here here http://www.red-gate.com/products/dotnet-development/pinvoke/

Download it and install it, close the visual studio and open it again, you can find the extension in the menu bar. enter image description here

If the error is due to the fact that the signature does not match you, just click on PInvoke.net> Insert PInvoke Signatures

The new window will look like this: enter image description here

Enter the dll name and click on the search, you will see all the functions of this DLL in the search results window. Click on the function in which you will receive a signature for this particular function.

Use this signature, and you need to change your programs in accordance with this Subscription, mainly a data type.

This solves my problem, you may have different problems, such as callConvention or additional attributes that must be specified when importing dll.

Happy coding. Be healthy!

+8
Sep 04 '14 at 10:21
source share

I had this problem when using VS2010. What it is: Visual Studio by default uses 64-bit code for "any processor." Pointers to variables (like strings) now become 64-bit when calling external Dlls, where, since all your trusted and trusted Dlls use 32-bit pointers.

Do not assume that something is wrong with your DLLs, no.

Change your VS settings to generate X86 code like this (Express versions of C #)

  • go to Tools β†’ Options.
  • In the lower left corner of the Options dialog box, select the Show All Settings check box.
  • In the tree on the left side, select "Projects and Solutions."
  • In the options on the right, select the "Show advanced configuration configurations" checkbox.
  • Click OK.
  • Go to build -> Configuration Manager ...
  • In the Platform column next to your project, click the combo box and select ".
  • In the "New Platform" setting, select "x86".
  • Click OK.
  • Click "Close."

I also notice that even if computers double their capacity every 12 months, my current computer with 1 gigabyte of RAM seems no faster than my first 486 with 4 megagrams. Don’t worry about using 64-bit code, it won’t be faster or better, because it is built on a huge cumbersome object-oriented inflation tower.

+2
Mar 01 2018-11-11T00:
source share

I tried calling the dll with CallingConvention is ThisCall and it worked for me. Here is my code working with BLOB MS Sql Server.

 [DllImport("sqlncli11.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.ThisCall)] private static extern SafeFileHandle OpenSqlFilestream( string FilestreamPath, UInt32 DesiredAccess, UInt32 OpenOptions, byte[] FilestreamTransactionContext, UInt32 FilestreamTransactionContextLength, Int64 AllocationSize); 

More at: https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention(v=vs.110).aspx

0
May 09 '16 at 4:23
source share



All Articles