https://codeoncode.blogspot.com/2016/12/create-job-object-and-terminate-child.html
Create a Job object And terminate Child Process
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE flag
How to gracefully handle when main.exe terminated, child.exe terminated 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.
unit JobsApi;
interface
uses
Windows;
type
TJobObjectInfoClass = Cardinal;
PJobObjectAssociateCompletionPort = ^TJobObjectAssociateCompletionPort;
TJobObjectAssociateCompletionPort = Record
CompletionKey : Pointer;
CompletionPort : THandle;
End;
{$EXTERNALSYM JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION}
JOB_OBJECT_LIMIT_BREAKAWAY_OK = $00000800;
{$EXTERNALSYM JOB_OBJECT_LIMIT_BREAKAWAY_OK}
JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK = $00001000;
{$EXTERNALSYM JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK}
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = $00002000;
{$EXTERNALSYM JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE}
Function QueryInformationJobObject(hJob : THandle;
JobObjectInformationClass : TJobObjectInfoClass;
lpJobObjectInformation : Pointer;
cbJobObjectInformationLength : DWORD;
lpReturnLength : PDWORD) : Bool; StdCall;
External Kernel32 Name 'QueryInformationJobObject';
Function SetInformationJobObject(hJob : THandle;
JobObjectInformationClass : TJobObjectInfoClass;
lpJobObjectInformation : Pointer;
cbJobObjectInformationLength : DWORD): BOOL; StdCall;
External Kernel32 Name 'SetInformationJobObject';
QueryInformationJobObject function (jobapi2.h)
https://docs.microsoft.com/en-us/windows/win32/api/jobapi2/nf-jobapi2-queryinformationjobobject
https://www.google.com/search?q=github+Helper.System.JobObject.Header.pas
https://gist.github.com/JensMertelmeyer/5e23a5dccb59b2902f37c69e7f7fa4cd
@JensMertelmeyer
JensMertelmeyer/Helper.System.JobObject.Header.pas
CreateJobObjectW. KERNEL32.CreateMutexW. KERNEL32.CreateProcessW. KERNEL32.CreateThread. KERNEL32.DelayLoadFailureHook.
沒有留言:
張貼留言