I’m trying to be able to have a mouse click on current mouse position. While using code from this site.
I’m getting an error:
A call to PInvoke function ‘Program!Program.Program::mouse_event’ 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.at line:mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);.
I don’t understand the problem so what is the cause? How could I fix this?
My code:
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public void DoMouseClick()
{
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
private void tmrClickInterval_Tick(object sender, EventArgs e)
{
DoMouseClick();
}
Your DllImport signature is invalid.
The type of
dwFlags,dx,dy, anddwDatais documented asDWORD, which is a 32-bit unsigned integer. In C#,longrepresents a 64-bit signed integer. As such, you should useuint, which represents a 32-bit unsigned integer .The last parameter
dwExtraInfois a of typeULONG_PTR(pointer to an unsigned 32-bit integer), which corresponds toUIntPtrin C#.Try this: