2

I am getting a PInvokeStackImbalance: 'PInvokeStackImbalance was detected Message: A call to PInvoke function 'ConvertedClass::MapViewOfFile' 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.'

I am fairly new to DLL use, and just managed to work out a few tutorials today.

Any help would be appreciated.

using System.Runtime.InteropServices; //dll [DllImport("kernel32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)] public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, FileMapAccessRights dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, ulong dwNumberOfBytesToMap;) string szSharedMemory = "FUNKY_BUSINESS"; //other dll call is successful and returns value IntPtr hMem = OpenFileMapping(FileMapAccessRights.Write, FALSE, szSharedMemory); ///BOOM.. not this one IntPtr pvHead = MapViewOfFile(hMem, FileMapAccessRights.Write, 0, 0, 0); 

Edit: It was a bad argument.. The 5th arg should be UIntPtr instead of ulong. this is how i feel right now

4
  • "The 5th arg should be uint instead of ulong." No, it should be UIntPtr. Or you'll end up with the same problem when you switch to 64 bit process! Commented May 22, 2012 at 20:48
  • 1
    Actually, I realised SIZE_T is unsigned so to be 100% accurate, UIntPtr is the way to go. Commented May 22, 2012 at 20:52
  • By the way, you do know that .net 4 has added support for memory mapped files Commented May 22, 2012 at 21:16
  • I found that in reading, and in all honesty, I would have liked to do the most cutting edge stuff but I was given some c++ and am pretty new to programming (fresh diploma) and decided to port over what I had been given, since most if it is still greek to me. (ported over with stacks help ofcourse) Commented May 22, 2012 at 21:24

2 Answers 2

3

The final parameter is SIZE_T. That's unsigned, and 32 bits in a 32 bit process and 64 bits in a 64 bit process. So the best solution is to use UIntPtr for the final parameter.

I would use the following:

[DllImport("kernel32")] public static extern IntPtr MapViewOfFile( IntPtr hFileMappingObject, FileMapAccessRights dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap ); 

Your code uses ulong which is always 64 bits wide. And your process is a 32 bit process which explains why the P/invoke marshaller has detected a stack imbalance.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the correction - I saw the SIZE_T final parameter in MSDN, but hoped it wasn't an issue given the explicit "kernel32" use. I guess that doesn't really mean a 32-bit-only library?
@JonSkeet kernel32 is what that DLL was called in the 32 bit version of Win32 and when the 64 bit version of Win32 was introduced they decided to keep the name because it had been hard-coded everywhere. Same for user32, gdi32 etc. And then of course we have the fun that on a 64 bit system, system32 is the 64 bit system dir and syswow64 is the 32 bit system dir. And kernel32 isn't even the kernel, that's ntoskrnl.exe. Go figure! That said, keeping the same name makes it easy to single source code that works with both 32 and 64. MS really got that right.
Used UIntPtr.Zero in my method call which seemed to work fine. Thanks
Use UIntPtr.Zero, or a cast (UIntPtr)0
0

The 5th parameter should be an uint, not ulong.

public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, FileMapAccessRights dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap;) 

For P/Invoke, you can use sample code from pinvoke.net. http://www.pinvoke.net/default.aspx/kernel32.mapviewoffile

1 Comment

Unfortunately this is another instance of pinvoke.net being incorrect. The final parameter is SIZE_T which is pointer sized.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.