WinUser.HWND_MESSAGE is truncated to 32 bits on 64-bit JVMs (CreateWindowEx fails with ERROR_INVALID_WINDOW_HANDLE)
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 80/100
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from java-native-access/jna
-
MacFileUtils: FSPathMakeRef passes an int where the C function takes its FSRef output pointerMay be free again @VelpulaGangavardhan claimed this 39 days ago, and no pull request is open. Open
Difficulty 1/5 Under an hour Newbie friendliness 79/100
java-native-access/jna#1746 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 43/100
java-native-access/jna#1752 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 44/100
java-native-access/jna#1751 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
java-native-access/jna#1747 · 1 comment ·
-
`Native.typeOptions` never releases library interfaces, leaking classloadersPossibly taken @curforever claimed this 51 days ago. Open
Difficulty 4/5 1-2 days Newbie friendliness 48/100
java-native-access/jna#1742 ·
All issues in java-native-access/jna
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
erkamyaman/rust-anything#1381 ·
Maintainers usually reply within 1 day
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day