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?
Asked
Active
Viewed 658 times
2 Answers
5
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.

Hans Passant
- 922,412
- 146
- 1,693
- 2,536
-
Thanks, but I don't quite follow. The memory leak here as a result of closehandle(), not being called. Will it be cleared up once the app that did the shell call is closed properly? – jjb Nov 29 '09 at 17:44
-
Yes, Windows cleans up handles that were not closed. Don't let it get that far. – Hans Passant Nov 29 '09 at 18:01
-
wont just calling sendmessage Call SendMessage(intHandle, const_CLOSE, 0&, 0&) both clse the app and do the job closehandle() does at the same time? – jjb Nov 29 '09 at 18:03
3
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.

Roland Rabien
- 8,750
- 7
- 50
- 67
-
3
-
thanks for this but it seems that the handle won't be reused because it stays open until the app that did the shell command closes it. – jjb Nov 29 '09 at 18:25