Vista - GetForegroundWindow does not return correct process ID but parent process ID

When I call GetForegroundWindow from C #, I get the parent process ID of the explorer (I can see this from the process explorer), not the process ID of the app in the foreground.

Why is this and how do I get the correct process ID?

Malcolm

+1


source to share


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


Not sure what's going on here, but have you tried using the app as an administrator? Currently, there are many restrictions on what non-administrator applications can see in the rest of the system.



0


source







All Articles