jprbest
jprbest

Reputation: 737

Pinvoke stack imbalance detected

I have the following function:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, SPoint lParam);

It keeps complaining about Pinvoke stack imbalance when the following code executes:

SendMessage(EventRichTextBox.Handle, EM_GETSCROLLPOS, 0, OldScrollPoint);

What can cause that issue?

this is my SPoint

  private struct SPoint
    {
        public Int32 x;
        public Int32 y;
    }

and

SPoint OldScrollPoint = default(SPoint);

Upvotes: 0

Views: 3853

Answers (2)

David Heffernan
David Heffernan

Reputation: 612993

Can't say for sure, but one obvious possibility is that you are on a 64 bit machine and int is the wrong type for wParam. It needs to be a 64 bit value in a 64 bit process.

We also have no idea how you have declared SPoint. You are meant to pass a pointer to a POINT struct. It doesn't look as though you have done that.

The correct signature is:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(
    IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

Your edit clarifies that SPoint is a struct. This then is clearly wrong. You could simply pass the SPoint as an out parameter. That would be the simplest solution.

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(
    IntPtr hWnd, int Msg, IntPtr wParam, out SPoint lParam);

If you wanted a more general SendMessage signature then you should use IntPtr as I stated above and use Marshal.StructureToPtr.

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 941585

The wParam argument should be IntPtr. But that's not what triggers the MDA, lying about the argument type is fine but you do have to do it correctly. Structures are passed by reference in the Windows api, declare the lParam argument as ref SPoint. Or out if the structure is returned, the case for EM_GETSCROLLPOS.

Upvotes: 1

Related Questions