How can I read the window title with JNI or JNA?

南楼画角 提交于 2020-01-09 10:46:48

问题


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)....

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.


回答1:


In JNA:

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

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

To use it:

byte[] windowText = new byte[512];

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

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.

The documentation for GetWindowText function is available here in MSDN.

Documentation for JNA is available at jna.dev.java.net



来源:https://stackoverflow.com/questions/1173926/how-can-i-read-the-window-title-with-jni-or-jna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!