如何在拥有句柄时检查给定进程是否正在运行 [英] How to check if a given process is running when having its handle

查看:51
本文介绍了如何在拥有句柄时检查给定进程是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ShellExecuteEx 来启动一个应用程序.成功启动后,我读取 TShellExecuteInfo.hProcess 以获取已启动进程的句柄.

I am using ShellExecuteEx to start an application. After successful start im reading TShellExecuteInfo.hProcess to obtain a handle of the started process.

我想定期检查由我的应用程序启动的进程是否仍在运行.两个或多个同名进程可以同时运行,我想确保我的应用程序正在检查正确的进程.

I would like to periodically check if the process started by my application is still running. Two or more processess with the same name can run simultaneously and I want to be sure that my application is checking the correct process.

我希望能够在从 Windows 2000 到更高版本的 Windows 系统上执行此操作.

I want to be able to do this on Windows systems from Windows 2000 to above.

我知道有一个 WinAPI 函数 GetProcessId 正是我想要的,但它不支持 Windows 2000.

I know that there is WinAPI function GetProcessId which is doing exactly what I want but it does not support Windows 2000.

感谢您的回答.

推荐答案

致电 WaitForSingleObject 在该句柄上,并使用零超时参数.如果进程仍在运行,函数将返回Wait_Timeout;如果进程已经终止,那么它将返回 Wait_Object_0(因为进程终止会导致它的句柄变成 signaled.)

Call WaitForSingleObject on that handle, and use a timeout parameter of zero. If the process is still running, the function will return Wait_Timeout; if the process has terminated, then it will return Wait_Object_0 (because process termination causes its handles to become signaled.)

如果你想知道进程的退出状态是什么,那就调用GetExitCodeProcess.

If you want to know what the exit status of the process is, then call GetExitCodeProcess.

这篇关于如何在拥有句柄时检查给定进程是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆