Search code examples
windowsshellhwnd

How often are process handles reused


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?


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.