4

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

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);
    }
}
4

1 回答 1

8

在 Windows 中,几乎所有东西都是一个窗口。以下是您的代码的一些更改,将显示一些窗口标题/文本:

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);
    }
}

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

于 2010-12-18T16:37:33.750 回答