且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 JNI 或 JNA 读取窗口标题?

更新时间:2023-12-03 23:19:10

在 JNA 中:

public interface User32 extends StdCallLibrary {
    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

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

使用它:

byte[] windowText = new byte[512];

PointerType hwnd = ... // assign the window handle here.
User32.INSTANCE.GetWindowTextA(hwnd, windowText, 512);
System.out.println(Native.toString(windowText));

您可能希望为 HWND 使用正确的结构映射并允许 unicode 支持;您可以在 JNA 网站上找到该信息和更多示例.

You'll probably want to use the proper structure mappings for HWND and also allow unicode support; you can find that information and more examples on how to do that at the JNA website.

GetWindowText 函数的文档位于 MSDN.

The documentation for GetWindowText function is available here in MSDN.

jna.dev.java.net