// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; using System.Threading; using System.Runtime.InteropServices; namespace E { public class Program { [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern IntPtr GetActiveWindow(); [DllImport("user32.dll")] static extern IntPtr GetTopWindow(IntPtr hWnd); [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)] static extern uint GetWindowModuleFileName(IntPtr hwnd, [Out] StringBuilder lpszFileName, uint cchFileNameMax); public static string GetForegroundModuleFileName() { IntPtr win = GetForegroundWindow(); StringBuilder buf = new StringBuilder(); GetWindowModuleFileName(win, buf, 1024); return buf.ToString(); } public static string GetActiveModuleFileName() { IntPtr win = GetActiveWindow(); StringBuilder buf = new StringBuilder(); GetWindowModuleFileName(win, buf, 1024); return buf.ToString(); } public static string GetTopModuleFileName() { IntPtr win = GetTopWindow(IntPtr.Zero); StringBuilder buf = new StringBuilder(); GetWindowModuleFileName(win, buf, 1024); return buf.ToString(); } public static void Main(string[] args) { while(true) { Console.WriteLine(GetForegroundModuleFileName() + " " + GetActiveModuleFileName() + " " + GetTopModuleFileName()); Thread.Sleep(500); } } } }