I need to create a function that determines if a process is still active, based purely on the process id. The processes corresponding to that ID and the function being run below will only ever be associated with one user. This is what I've done:
bool SomeClass::IsProcIDValid()
{
int pid = atoi(ProcessID);
HANDLE processHandle = NULL;
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
if (processHandle)
{
CloseHandle(processHandle);
return true;
}
return false;
}
Note: ProcessID is a string member of SomeClass.
The code above is working exactly as I would like it to so far, however many of the other examples I've found (eg: here) recommend using GetExitCodeProcess, and I'm now questioning whether I should be using this instead.
Is there any reason why the above code would fail though? If I can't get access to the process handle, I'm using this as a proxy for the process no longer being active. If I can't get access to the handle then I can't use GetExitCodeProcess anyway.