Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a given process is running when having its handle

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.

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

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

Thank you for your answers.

like image 397
Wodzu Avatar asked Jun 27 '11 13:06

Wodzu


People also ask

How to check if a process is running or not in PowerShell?

Follow the simple guidelines for checking if a process is running or not. They are as such pinfo = proc. as_dict (attrs= [‘pid’, ‘name’, ‘create_time’]) except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :

How do I get the process handle of a process?

If you have a process identifier, you can get the process handle by calling the OpenProcess function. OpenProcess enables you to specify the handle's access rights and whether it can be inherited. A process can use the GetCurrentProcess function to retrieve a pseudo handle to its own process object.

What happens when checkifprocessrunning returns false in a while loop?

Once checkIfProcessRunning returns False, it sets chromeRunning = False to exit the while loop and continue with the rest of the program. You need to determine the process name you’re checking.

How to check if a process is running or not using psutil?

pinfo = proc. as_dict (attrs= [‘pid’, ‘name’, ‘create_time’]) except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) : To check if a process is running or not we will iterate over all the running processes using psutil.process_iter () and during iteration match the process name.


1 Answers

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.)

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

like image 138
Rob Kennedy Avatar answered Nov 02 '22 23:11

Rob Kennedy