首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WinAPI GetWindowInfo和GetWindowRect返回值为0

WinAPI GetWindowInfo和GetWindowRect返回值为0
EN

Stack Overflow用户
提问于 2021-06-02 01:03:10
回答 1查看 190关注 0票数 0

下面是正在发生的事情。我使用User32来使用FindWindowA函数,其中我的测试用例是记事本(即“无标题-记事本”)。然后,我使用了其他几个函数,如GetWindowInfo和GetWindowRect,返回的值都奇怪地都是0。下面是我在Java中所做的工作:

代码语言:javascript
运行
复制
Pointer windowHandle = user32.FindWindowA(null, "Untitled - Notepad");
System.out.println(windowHandle == null ? "Pointer is NULL" : "Pointer is valid.");

WinAPI.WINDOWINFO windowInfo = new WinAPI.WINDOWINFO();
Pointer windowInfoPtr = windowInfo.getPointer();
System.out.println("result of GetWindowInfo: " + user32.GetWindowInfo(windowHandle, windowInfoPtr));

System.out.println(windowInfo.cbSize);
System.out.println(windowInfo.dwWindowStatus);
System.out.println(windowInfo.dwStyle);

WinAPI.RECT rcWindow = windowInfo.rcWindow;
System.out.println(rcWindow.bottom.longValue() + ", " + rcWindow.left.longValue() + ", " + rcWindow.right.longValue() + ", " + rcWindow.top.longValue());

WinAPI.RECT r = new WinAPI.RECT();
Pointer rPtr = r.getPointer();

System.out.println("result of GetWindowRect: " + user32.GetWindowRect(windowHandle, rPtr));

System.out.println(r.top + ", " + r.left + ", " + r.bottom + ", " + r.right);

产出的结果如下:

代码语言:javascript
运行
复制
Pointer is valid.
result of GetWindowInfo: true
60
0
0
0, 0, 0, 0
result of GetWindowRect: true
0, 0, 0, 0

我使用记事本作为测试用例,因为它只是一个基本的、一个窗口的程序。为什么每个人的值都是"0“?窗口句柄显然不是空的,理论上应该有效。

为了获得更多的信息,下面是我为JNA制作的结构,这样它就可以映射它们:

代码语言:javascript
运行
复制
    public static class WINDOWINFO extends Structure {
        public int cbSize = this.size();
        public RECT rcWindow;
        public RECT rcClient;
        public int dwStyle;
        public int dwExStyle;
        public int dwWindowStatus;
        public int cxWindowBorders;
        public int cyWindowBorders;
        public short atomWindowType;
        public short wCreatorVersion;

        public WINDOWINFO() {
            super();
        }

        public WINDOWINFO(Pointer p) {
            super(p);
        }

        protected List<String> getFieldOrder() {
            return Arrays.asList(new String[] {"cbSize", "rcWindow", "rcClient", "dwStyle", "dwExStyle", "dwWindowStatus", "cxWindowBorders", "cyWindowBorders", "atomWindowType", "wCreatorVersion"});
        }
    }

    public static class RECT extends Structure {
        public NativeLong left;
        public NativeLong top;
        public NativeLong right;
        public NativeLong bottom;

        public RECT() {
            super();
        }

        public RECT(Pointer p) {
            super(p);
        }

        protected List<String> getFieldOrder() {
            return Arrays.asList(new String[] {"left", "top", "right", "bottom"});
        }
    }

我正在使用Java和JNA库使用WinAPI。User32用于调用上述调用。

任何帮助都将不胜感激。我找不到很多关于为什么会发生这种情况的信息(即使是在WinAPI文档上)。谢谢。

EN

Stack Overflow用户

回答已采纳

发布于 2021-06-02 01:14:27

您正在创建类的实例,然后手动调用类上的getPointer(),将本机内存分配给Win32 API以进行填充,但之后您不会将该本机内存返回到类成员中,这就是为什么函数退出后成员都保持为零的原因。

根据getPointer()文档:

Pointer对象返回到此结构。注意,如果您使用结构的指针作为函数参数,则负责在调用之前调用 ,并在调用后调用 。当Function对象遇到Structure参数或返回值时,通常会自动处理这些调用。返回的指针可能对Structure.ByValue结构表示没有意义。

例如:

代码语言:javascript
运行
复制
WinAPI.WINDOWINFO windowInfo = new WinAPI.WINDOWINFO();
windowInfo.write(); // <-- add this
System.out.println("result of GetWindowInfo: " + user32.GetWindowInfo(windowHandle, windowInfo.getPointer()));
windowInfo.read(); // <-- add this
...
WinAPI.RECT r = new WinAPI.RECT();
//r.write(); // <-- optional in this case
System.out.println("result of GetWindowRect: " + user32.GetWindowRect(windowHandle, r.getPointer()));
r.read(); // <-- add this

否则,您应该将user32.GetWindowInfo()的声明更改为接受WinAPI.WINDOWINFO参数,而不是Pointer参数。user32.GetWindowRect()WinAPI.RECT也是如此。让JNA为您处理本机内存的序列化。

票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67797679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档