Vista - GetForegroundWindow不会返回正确的进程ID,而是父进程ID

Mal*_*olm 1 c# windows-vista

当我从c#调用GetForegroundWindow时,我获取了资源管理器父进程ID(我从进程资源管理器中看到了这一点),而不是前台应用程序的进程ID.

为什么这样以及如何获得正确的进程ID?

马尔科姆

Wol*_*lf5 5

API函数GetForegroundWindow为您提供顶部窗口的句柄,而不是进程ID.那么你用什么其他函数从GetForegroundWindow获得的窗口句柄中获取进程ID?

这将获得前景窗口的WINDOW HANDLE:

    [DllImport("user32", SetLastError = true)]
    public static extern int GetForegroundWindow();
Run Code Online (Sandbox Code Playgroud)

这将获得给定WINDOW HANDLE的进程ID(taskmgr中的PID):

    [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;
    }
Run Code Online (Sandbox Code Playgroud)

如果你回答你的问题会很好,所以它在这个论坛上有一些价值.