简体   繁体   中英

CreateProcess handling

I use Windows API CreateProcess function. In the msdn manual for it, I have found

If the function succeeds, be sure to call the CloseHandle function to close the hProcess and hThread handles when you are finished with them. Otherwise, when the child process exits, the system cannot clean up the process structures for the child process because the parent process still has open handles to the child process.

So where I should do this?

The situation is that the process is started and there is no track of his life. Does it force me to make a thread, where the process will be created and then the thread will wait using, for example WaitForSingleObject , until the process will be dead, so the handles could be released?

You don't have to wait for the child process to finish, you just have to CloseHandle when you no longer need the handle.

The reason for this is that you may want to keep a handle to the process even after the process has finished. You may want to check its return status, for example. As long as you have a handle to it, Windows can't clean up after it.

But if you don't care anymore what the child does, close the handle and move on.

If you do care, call WaitForSingleObject and give it the handle you got from CreateProcess . Or call RegisterWaitForSingleObject (again passing it the process handle) with a callback function that will be called when the process ends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM