Question

I note an applications handle when I use the shell function to open it. I then use that handle to close the application later. However the user can also close that other application himself. Can that handle then be reused by windows so that when I use that handle I close a different process. If it is possible is it likely?

Was it helpful?

Solution

No, you don't have to worry about it. The handle returned by, say, OpenProcess, ShellExecuteEx() or CreateProcess keeps the process object alive. That's how it is possible to call GetExitCodeProcess() to retrieve the exit code after the process is terminated.

The object doesn't get released until the last handle on it is closed. Opposite of earlier advice given in this thread, it is very important that you call CloseHandle() or you'll have a leak.

OTHER TIPS

You can wait on a process handle to figure out when it is exited.

WaitForSingleObject(hProcess, INFINITE);

Once this returns, you know the process has exited and you don't need to close it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top