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

未关闭 适合新手
#1,757 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
80/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java
领域
desktop

调研方向

从 WinUser 中 HWND_MESSAGE 的声明开始,并比较 issue 中描述的 Pointer.createConstant 重载。检查其他负数句柄常量是否如建议所述使用 int 重载。完成标准是能够在 64 位 JVM 上创建仅接收消息的窗口,并验证受影响的常量。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Java
星标
8.9k
派生
1.7k
平均合并
3 小时 57 分钟
30 天内合并 PR
1

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

java-native-access/jna 的其他 Issue

查看 java-native-access/jna 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。