Vista - GetForegroundWindow does not return correct process ID but parent process ID
2 answers
The GetForegroundWindow API function gives you a handle to the top window, not a process ID. So what other functions do you use to get the process ID from the window handle from GetForegroundWindow?
This will open you up with a WINDOW of the foreground window:
[DllImport("user32", SetLastError = true)]
public static extern int GetForegroundWindow();
This will give you the process id (PID in taskmgr) of the given WINDOW HANDLE:
[DllImport("user32", SetLastError = true)]
public static extern int GetWindowThreadProcessId(int hwnd, ref int lProcessId);
public static int GetProcessThreadFromWindow(int hwnd) {
int procid = 0;
int threadid = GetWindowThreadProcessId(hwnd, ref procid);
return procid;
}
It would be nice if you answered your own question, so it has some value in this forum.
+5
source to share