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);
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
.