How often are window handlers reused

I notice that it processes applications when I use the shell function to open it. Then I use this handle to close the application later. However, the user may also close this other application. Can this handle be reused by windows, so when I use this handle I close another process. If possible, is it possible?

+3
source share
2 answers

No, you have nothing to worry about. A handler returned, for example, by OpenProcess, ShellExecuteEx (), or CreateProcess, keeps the process object active. How you can call GetExitCodeProcess () to retrieve the exit code after the process terminates.

An object is not freed until the last handle on it is closed. Unlike the previous tips provided in this thread, it is very important that you call CloseHandle () or you have a leak.

+5
source

You can wait for the process handle to detect when it exits.

WaitForSingleObject (hProcess, INFINITE);

As soon as this returns, you know that the process has completed and you do not need to close it.

+3
source

Source: https://habr.com/ru/post/1724213/


All Articles