使用JNA和EnumWindows的问题 [英] problem using JNA and EnumWindows

查看:129
本文介绍了使用JNA和EnumWindows的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JNA,这是我尝试运行的第一个程序。我从参考文件中复制了它,但是,当我运行它时,他找到了412个窗口......我很确定我现在没有那么多窗口打开了:)可以请有人向我解释程序的行为吗?

I'm experimenting with JNA, and this is the first program I try to run. I copied it from the reference, but, when i run it, he finds 412 windows ... and I'm quite sure I've not that many window opened right now :) Can please someone explain me the behaviour of the program?

import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;

public class Main {
// Equivalent JNA mappings
    public interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

        interface WNDENUMPROC extends StdCallCallback {
            boolean callback(Pointer hWnd, Pointer arg);
        }

        boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
    }

    public static void main(String[] args) {
        User32 user32 = User32.INSTANCE;

        user32.EnumWindows(new User32.WNDENUMPROC() {
            int count;
            public boolean callback(Pointer hWnd, Pointer userData) {
                System.out.println("Found window " + hWnd + ", total " + ++count);
                return true;
            }
        }, null);
    }
}


推荐答案

In Windows,几乎所有东西都是Window。以下是对代码的一些更改,这些更改将显示一些窗口标题/文本:

In Windows, almost everything is a Window. Here are some changes to your code that will show some of the window titles/text:

import com.sun.jna.Pointer;
import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;

public class JNA_Main {
    // Equivalent JNA mappings
    public interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

        interface WNDENUMPROC extends StdCallCallback {
            boolean callback(Pointer hWnd, Pointer arg);
        }

        boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);

        int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount);
    }

    public static void main(String[] args) {
        final User32 user32 = User32.INSTANCE;

        user32.EnumWindows(new User32.WNDENUMPROC() {

            int count;

            public boolean callback(Pointer hWnd, Pointer userData) {
                byte[] windowText = new byte[512];
                user32.GetWindowTextA(hWnd, windowText, 512);
                String wText = Native.toString(windowText);
                wText = (wText.isEmpty()) ? "" : "; text: " + wText;
                System.out.println("Found window " + hWnd + ", total " + ++count + wText);
                return true;
            }
        }, null);
    }
}

请询问是否有任何不清楚的地方。

Please ask if anything is unclear.

这篇关于使用JNA和EnumWindows的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆