当我从c#调用GetForegroundWindow时,我得到的父进程ID(我从进程资源管理器中看到这个),而不是进入的应用程序的进程ID前景。Vista - GetForegroundWindow不返回正确的进程ID,但父进程ID
这是为什么?以及如何获得正确的进程ID?
马尔科姆
当我从c#调用GetForegroundWindow时,我得到的父进程ID(我从进程资源管理器中看到这个),而不是进入的应用程序的进程ID前景。Vista - GetForegroundWindow不返回正确的进程ID,但父进程ID
这是为什么?以及如何获得正确的进程ID?
马尔科姆
不知道这里发生了什么,但你有没有试图运行你的应用程序作为管理员?现在有很多关于非管理应用程序能够在整个系统的其他部分看到的限制。
API函数GetForegroundWindow让您获得顶层窗口的句柄,而不是进程ID。 那么你用什么其他函数从你从GetForegroundWindow获得的窗口句柄中获得进程ID?
这将让你在前台窗口的窗口句柄:一个给定的窗口句柄
[DllImport("user32", SetLastError = true)]
public static extern int GetForegroundWindow();
这将让你的进程ID(PID在taskmgr):
[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;
}
这如果你对你的问题做出回应,那么它就会很好,所以它在这个论坛上有一定的价值。