I want my program to trigger an action only when certain other programs are currently being used. I get the current foreground HWND with GetForegroundWindow()
. But HWNDs change over time, so that's not a way to identify those programs. The same goes for Process IDs and handles. What is a way to identify the foreground program over reboots?
I first thought GetModuleFileNameExA
should work, but my code crashes because it is not found:
Handle handle = GetProcessHandleFromHwnd(hWID);
String Name = null;
GetModuleFileNameExA(
handle,
null,
Name,
2147483647
);
[DllImport("Kernel32.dll", CharSet = CharSet.Ansi)]
private static extern IntPtr GetModuleFileNameExA(
Handle hProcess,
Object hModule,
String lpFilename,
Int32 nSize
);