Skip to content

User32: SetCursorPos is declared (long, long), but Windows declares (int, int) #1745

Open

Description

@GreenFuze

User32.SetCursorPos takes two Java longs. Win32 declares both coordinates as int.

// contrib/platform/src/com/sun/jna/platform/win32/User32.java:2329
boolean SetCursorPos(long x, long y);
// um/WinUser.h (Windows SDK 10.0.28000)
WINUSERAPI
BOOL
WINAPI
SetCursorPos(
    _In_ int X,
    _In_ int Y);

What it can cause

On 64-bit Windows nothing goes wrong: each argument travels in a register, and SetCursorPos reads its low 32 bits. On 32-bit Windows the arguments go on the stack, and each long takes 8 bytes. SetCursorPos reads X from the low half of x and Y from the high half of x, which is 0 for any on-screen x, so the cursor lands at (x, 0). Because the function is stdcall, it also pops 8 bytes where 16 were pushed. I don't have 32-bit Windows to run this; it follows from the two declarations. User32Test.testSetCursorPos passes on 64-bit, which is consistent with it.

Fix

boolean SetCursorPos(int x, int y);. Changing the signature breaks callers compiled against the long version, so adding the int overload and deprecating the long one may be gentler. Happy to send a PR for whichever you prefer.

How this was found

By a research tool that compares each native declaration against the C declaration of the function it binds. Nothing was built or executed; every file quoted is in this repository at 5.19.1, and unchanged on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions