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);
}
}