My application (main.exe) is executing a Child process (child.exe) using ShellExecuteEx.
But when I close or kill (via Process-Explorer) main.exe the child process remains active.
How to gracefully handle that, when main.exe terminates child.exe terminates also?
You need to use jobs. Main executable should create a job object, then you’ll need to set JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE flag to your job object.
Then you need to execute another process with CreateProcess function where
dwCreationFlagsmust be set toCREATE_BREAKAWAY_FROM_JOB. If this function succeeds callAssignProcessToJobObject.When all of this done all the child processes will be automatically terminated even if the main executable has been killed. You can get the JobsApi unit here. Note: I’ve not tested it with Delphi 7.
EDIT: Here you can download working demo project.