Assuming I've got these two facts right :-
CreateProcess()
starts a process and lets your program continue while it runs, but when your program finishes, the child process goes down with it. Furthermore, your program has to take note of when the child process exits so that it can properly release the process handle.
system()
starts a process, waits for it to complete then continues with your program.
So is it, please, actually possible to do that?
You can specify "DETACHED_PROCESS" in CreationFlags Like the following code snippent:
if (CreateProcessW(NULL, (LPWSTR) L"File.exe ",
0, 0, false,CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW | DETACHED_PROCESS , 0, 0,
&siStartupInfo, &piProcessInfo) != false)
{
/* Watch the process. */
dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (2 * 1000));
}
else
{
/* CreateProcess failed */
iReturnVal = GetLastError();
}
This will make yur processes independent. I hope this helps you.
CreateProcess()
does not wait for child process to finish by default, it returns immediately. Use WaitForSingleObject
if you want to wait.
No, the child process is not killed automatically when the parent exits, and the handle is anyway freed automatically by the operating system, so no need to do it yourself (if you use a modern version of windows).
Just like any OS resource (gdi objects, user objects, kernel objects), if you don't destroy/release/close the resource yourself then you will have resource leaks while your application runs. You should close both handles returned from CreateProcess
soon after CreateProcess
returns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With