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

后端 未结 1 757
耶瑟儿~
耶瑟儿~ 2020-12-10 17:35

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

相关标签:
1条回答
  • 2020-12-10 18:20

    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

    0 讨论(0)
提交回复
热议问题