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

2022-01-25 00:00:00 swig winapi java java-native-interface jna

希望重返开发领域;主要是使用 Java 调用一些原生的 win32 函数(我不想在 .NET 中构建)....

Looking to get back into the development space; primarily using Java to call some native win32 functions (I don't desire to build in .NET)....

谁能指出我可以使用 Java (JNI/JNA/SWIG) 从不同的运行窗口中读取标题的地方.假设您知道您尝试挂接的应用程序在内存空间的哪个位置.

Can someone point me to a place where I can read the title from a differnt running window using Java (JNI/JNA/SWIG). Assume you would know where in the memory space the application you are attempting to hook into is.

推荐答案

在 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

相关文章