diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..910969b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.project +*.classpath +/bin/* +/.settings/* +/.externalToolBuilders/* +*.log \ No newline at end of file diff --git a/ActiveWindowCheck.jar b/ActiveWindowCheck.jar new file mode 100644 index 0000000..669c029 Binary files /dev/null and b/ActiveWindowCheck.jar differ diff --git a/lib/jna-4.3.0.jar b/lib/jna-4.3.0.jar new file mode 100644 index 0000000..713354e Binary files /dev/null and b/lib/jna-4.3.0.jar differ diff --git a/lib/jna-platform-4.3.0.jar b/lib/jna-platform-4.3.0.jar new file mode 100644 index 0000000..16d3ae6 Binary files /dev/null and b/lib/jna-platform-4.3.0.jar differ diff --git a/run.cmd b/run.cmd new file mode 100644 index 0000000..cb23b7a --- /dev/null +++ b/run.cmd @@ -0,0 +1 @@ +java -jar ActiveWindowCheck.jar > activewindow.log \ No newline at end of file diff --git a/src/ru/diowo/activewindowcheck/Main.java b/src/ru/diowo/activewindowcheck/Main.java new file mode 100644 index 0000000..0e0741a --- /dev/null +++ b/src/ru/diowo/activewindowcheck/Main.java @@ -0,0 +1,71 @@ +package ru.diowo.activewindowcheck; + +import com.sun.jna.Native; + +import com.sun.jna.Pointer; +import com.sun.jna.platform.win32.WinDef.HWND; +import com.sun.jna.platform.win32.WinUser; +import com.sun.jna.ptr.IntByReference; +import com.sun.jna.win32.StdCallLibrary; + +import com.sun.jna.Native; +import com.sun.jna.platform.win32.Kernel32; +import com.sun.jna.platform.win32.User32; +import com.sun.jna.platform.win32.WinDef.HWND; +import com.sun.jna.platform.win32.WinDef.RECT; +import com.sun.jna.platform.win32.WinNT.HANDLE; + +import java.util.Timer; +import java.util.TimerTask; + +public class Main { + + private static String lastWindow = ""; + + private static final int MAX_TITLE_LENGTH = 1024; + + public interface PsApi extends StdCallLibrary { + + int GetModuleFileNameExA(HANDLE process, HANDLE module, byte[] name, int i); + + } + + public static void main(String[] args) throws Exception { + + Timer timer = new Timer(); + TimerTask task = new TimerTask() { + @Override + public void run() { + PsApi psapi = (PsApi) Native.loadLibrary("psapi", PsApi.class); + + char[] buffer = new char[MAX_TITLE_LENGTH * 2]; + HWND hwnd = User32.INSTANCE.GetForegroundWindow(); + User32.INSTANCE.GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH); + String currWindow = Native.toString(buffer); + if (! lastWindow.equals(currWindow)) { + lastWindow = currWindow; + + System.out.println("Active window title: " + Native.toString(buffer)); + RECT rect = new RECT(); + User32.INSTANCE.GetWindowRect(hwnd, rect); + System.out.println("rect = " + rect); + + IntByReference pid = new IntByReference(); + User32.INSTANCE.GetWindowThreadProcessId(hwnd, pid); + + byte[] filename = new byte[1024]; + + HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, pid.getValue()); + psapi.GetModuleFileNameExA(process, null, filename, 1024); + String filenameString= Native.toString(filename); + + System.out.println("PID "+pid.getValue()+", filename "+filenameString); + } + } + }; + + timer.schedule(task, 1000, 500); + + } + +} diff --git a/workspace.xml b/workspace.xml new file mode 100644 index 0000000..43bdcfe --- /dev/null +++ b/workspace.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + +