/** * Uses JNA to retrieve the module file name of a program and parses it as a string which is then * passed to the InterruptionLogic class. */ private void getProgramIdentifier() { PSAPI psapi = (PSAPI) Native.loadLibrary("psapi", PSAPI.class); HWND focusedWindow = User32.INSTANCE.GetForegroundWindow(); byte[] name = new byte[1024]; IntByReference pid = new IntByReference(); User32.INSTANCE.GetWindowThreadProcessId(focusedWindow, pid); System.out.println("pid = " + pid.getValue()); HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, pid.getValue()); // Initally I was using the 'GetWindowModuleFileName' which provided // erronous behaviour. 'technomage' on Stackoverflow pointed out that // the method that should be used is 'GetModuleFileNameEx' - // http://stackoverflow.com/questions/15693210/getmodulefilename-for-window-in-focus-jna-windows-os psapi.GetModuleFileNameExA(process, null, name, 1024); String nameString = Native.toString(name); System.out.println(nameString); interruptDec.interruptNow(nameString); }
public static Window getForegroundWindow() { return new Window(User32.INSTANCE.GetForegroundWindow()); }