Search code examples
javajava-native-interfacejna

How to return HWND in JAVA


The attached code search for a window according to its title and activate it if exist.

   public static void ActivateWindow() {
  User32.INSTANCE.EnumWindows(new WNDENUMPROC() {

     @Override
     public boolean callback(HWND hWnd, Pointer arg1) {
        char[] windowText = new char[512];
        User32.INSTANCE.GetWindowText(hWnd, windowText, 512);
        String wText = Native.toString(windowText);

        String title = "Chrome";
            if (wText.contains(title))
            {
            User32.INSTANCE.ShowWindow(hWnd, 9); //activeWindow()
            User32.INSTANCE.SetForegroundWindow(hWnd);   //activeWindow()
            }

        return true;   

     }
  }, null);
}

My goal is to split ActivateWindow() method .

IsWindowOpen() will return HWND object if window exist, and activateWindow() will activate the HWND.

I cannot find a way to return HWND within a callback?


Solution

  • There are at least two ways

    Using instance variables:
    If you make your methods non-static, you can access instance variables inside your callback.
    Look at the usages of foundWindow in the following example:

    public class JNA_Test {
        HWND foundWindow = null;
    
        public static void main(String[] args) {
            JNA_Test jna = new JNA_Test();
            if(jna.isWindowOpen("chrome")){
                jna.activateWindow();
            }
        }
    
        public void activateWindow() {
            if(foundWindow != null) {
                User32.INSTANCE.ShowWindow(foundWindow, 9);
                User32.INSTANCE.SetForegroundWindow(foundWindow);
            }
        }
    
        public boolean isWindowOpen(String title) {
            foundWindow = null;
            User32.INSTANCE.EnumWindows(new WNDENUMPROC() {
    
                @Override
                public boolean callback(HWND hWnd, Pointer arg1) {
                    if(foundWindow == null) {
                        char[] windowText = new char[512];
                        User32.INSTANCE.GetWindowText(hWnd, windowText, 512);
                        String wText = Native.toString(windowText);
                        if (wText.contains(title)) {
                            foundWindow = hWnd;
                        }
                    }
                    return true;
    
                }
            }, null);
    
            return foundWindow != null;
        }
    }
    


    Using JNA Pointer:
    In this example it is no matter whether the methods are static or not.
    Look at the usages of foundWindowPointer:

    public class JNA_Test2 {
    
        public static void main(String[] args) {
            Pointer foundWindowPointer = new Memory(Pointer.SIZE);
            JNA_Test2.isWindowOpen("chrome", foundWindowPointer);
            if (foundWindowPointer.getPointer(0) != null) {
                HWND foundWindow = new HWND(foundWindowPointer.getPointer(0));
                JNA_Test2.activateWindow(foundWindow);
            }
        }
    
        public static void activateWindow(HWND foundWindow) {
            if (foundWindow != null) {
                User32.INSTANCE.ShowWindow(foundWindow, 9);
                User32.INSTANCE.SetForegroundWindow(foundWindow);
            }
        }
    
        public static void isWindowOpen(String title, Pointer foundWindowPointer) {
            User32.INSTANCE.EnumWindows(new WNDENUMPROC() {
    
                @Override
                public boolean callback(HWND hWnd, Pointer foundWindowPointer) {
                    if (foundWindowPointer != null) {
                        char[] windowText = new char[512];
                        User32.INSTANCE.GetWindowText(hWnd, windowText, 512);
                        String wText = Native.toString(windowText);
                        if (wText.contains(title)) {
                            foundWindowPointer.setPointer(0, hWnd.getPointer());
                        }
                    }
                    return true;
    
                }
            }, foundWindowPointer);
        }
    }