WinUser.HWND_MESSAGE is truncated to 32 bits on 64-bit JVMs (CreateWindowEx fails with ERROR_INVALID_WINDOW_HANDLE)

Open Beginner friendly
#1,757 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
80/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java
Domain
desktop

Research direction

Start with the HWND_MESSAGE declaration in WinUser and compare the Pointer.createConstant overloads described in the issue. Check whether other negative handle constants use the int overload as suggested. Done when the message-only window can be created on 64-bit JVMs and the affected constants are verified.

Written by the indexing model from the issue text.

Description

Description

WinUser.HWND_MESSAGE is declared as:

HWND HWND_MESSAGE = new HWND(Pointer.createConstant(-3));

Pointer.createConstant(int) masks the value with 0xFFFFFFFFL, so on a 64-bit JVM the handle becomes 0x00000000FFFFFFFD instead of 0xFFFFFFFFFFFFFFFD ((HWND)-3). Windows rejects it as a parent window, so a message-only window cannot be created with this constant.

Environment
  • jna / jna-platform 5.19.1 (the declaration is unchanged on master)
  • Windows 11 x64, 64-bit JVM (Zulu 11)
Steps to reproduce
WinUser.WNDCLASSEX wc = new WinUser.WNDCLASSEX();
wc.hInstance = Kernel32.INSTANCE.GetModuleHandle(null);
wc.lpfnWndProc = (WinUser.WindowProc) (h, msg, wp, lp) -> User32.INSTANCE.DefWindowProc(h, msg, wp, lp);
wc.lpszClassName = "HwndMessageTest";
User32.INSTANCE.RegisterClassEx(wc);

WinDef.HWND hwnd = User32.INSTANCE.CreateWindowEx(0, "HwndMessageTest", "", 0, 0, 0, 0, 0,
        WinUser.HWND_MESSAGE, null, wc.hInstance, null);
System.out.println(hwnd + " error=" + Native.getLastError());
Expected

A message-only window handle is returned.

Actual

hwnd is null and Native.getLastError() returns 1400 (ERROR_INVALID_WINDOW_HANDLE).

Passing new WinDef.HWND(Pointer.createConstant(-3L)) instead works. The window is created, and WTSRegisterSessionNotification / WM_WTSSESSION_CHANGE work as expected.

Suggested fix

Use the long overload, so the value is sign-extended on 64-bit and still correct on 32-bit:

HWND HWND_MESSAGE = new HWND(Pointer.createConstant(-3L));

Other handle constants in jna-platform that are built with createConstant(int) and a negative value may have the same problem and could be checked too.

Dominant language
Java
Stars
8.9k
Forks
1.7k
Avg merge
3h 57m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from java-native-access/jna

All issues in java-native-access/jna

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.