1

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?

Vitalii Pro
  • 313
  • 2
  • 17
shloro
  • 115
  • 1
  • 9

2 Answers2

2

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);
    }
}
Gren
  • 1,850
  • 1
  • 11
  • 16
1

As HWND is actually a "pointer to something", i.e. a void* you may want to use jna's com.sun.jna.Pointer type on the Java side.

An even better fit is com.sun.jna.platform.win32.WinDef.HWND when using jna's win32 platform API as shown in the answer to Handle external windows using java. Look at How to get list of all window handles in Java (Using JNA)? for another code example.

Community
  • 1
  • 1
Hille
  • 4,096
  • 2
  • 22
  • 32