Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Child process depending on it's Parent?

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?

like image 460
ZigiZ Avatar asked Jan 11 '12 15:01

ZigiZ


2 Answers

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.

uses
  JobsApi;
//...
var
  jLimit: TJobObjectExtendedLimitInformation;

  hJob := CreateJobObject(nil, PChar('JobName');
  if hJob <> 0 then
  begin
    jLimit.BasicLimitInformation.LimitFlags := JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
      SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, @jLimit,
        SizeOf(TJobObjectExtendedLimitInformation));
  end; 

Then you need to execute another process with CreateProcess function where dwCreationFlags must be set to CREATE_BREAKAWAY_FROM_JOB. If this function succeeds call AssignProcessToJobObject.

function ExecuteProcess(const EXE : String; const AParams: string = ''; AJob: Boolean = True): THandle;
var
  SI : TStartupInfo;
  PI : TProcessInformation;
  AFlag: Cardinal;
begin
  Result := INVALID_HANDLE_VALUE;
  FillChar(SI,SizeOf(SI),0);
  SI.cb := SizeOf(SI);

  if AJob then
    AFlag := CREATE_BREAKAWAY_FROM_JOB
  else
    AFlag := 0;


  if CreateProcess(
     nil,
     PChar(EXE + ' ' + AParams),
     nil,
     nil,
     False,
     AFlag,
     nil,
     nil,
     SI,
     PI
     ) then
  begin
   { close thread handle }
    CloseHandle(PI.hThread);
    Result := PI.hProcess;
  end;
end;
//...
  hApp := ExecuteProcess('PathToExecutable');

  if hApp <> INVALID_HANDLE_VALUE then
  begin
     AssignProcessToJobObject(hJob, hApp);
  end;

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.

like image 58
Linas Avatar answered Oct 22 '22 19:10

Linas


Try using Job Objects , check these functions CreateJobObject and AssignProcessToJobObject.

A job object allows groups of processes to be managed as a unit. Job objects are namable, securable, shareable objects that control attributes of the processes associated with them. Operations performed on a job object affect all processes associated with the job object. Examples include enforcing limits such as working set size and process priority or terminating all processes associated with a job.

like image 3
RRUZ Avatar answered Oct 22 '22 19:10

RRUZ