2022年5月24日 星期二

一起結束

 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.

提升權限

 OpenProcessToken GetCurrentProcessID   TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY

 CreateProcessAsUser

SystemHandleInformation = 16;
ProcessBasicInformation = 0;
STATUS_SUCCESS = cardinal($00000000);
SE_DEBUG_PRIVILEGE =20;
STATUS_ACCESS_DENIED = cardinal($C0000022);
STATUS_INFO_LENGTH_MISMATCH = cardinal($C0000004);
SEVERITY_ERROR = cardinal($C0000000);
TH32CS_SNAPPROCESS = $00000002;  
JOB_OBJECT_ALL_ACCESS = $1f001f;

printf("SeCreateSymbolicLinkPrivilege = %ld, %ld\n", seCreateSymbolicLinkPrivilege.HighPart, seCreateSymbolicLinkPrivilege.LowPart);

      if (!GetTokenInformation(hProcess, TokenPrivileges, NULL, 0, &length))
      {
        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
          TOKEN_PRIVILEGES* privileges = (TOKEN_PRIVILEGES*)malloc(length);
          if (GetTokenInformation(hProcess, TokenPrivileges, privileges, length, &length))
          {
            BOOL found = FALSE;
            DWORD count = privileges->PrivilegeCount;

            printf("User has %ld privileges\n", count);

            if (count > 0)
            {
              LUID_AND_ATTRIBUTES* privs = privileges->Privileges;
              while (count-- > 0 && !luid_eq(privs->Luid, seCreateSymbolicLinkPrivilege))
                privs++;
              found = (count > 0);

advapi32.dll advapi32.lib AbortSystemShutdown InitiateSystemShutdown 關機 重新 開機

 https://docs.microsoft.com/zh-tw/windows/win32/shutdown/displaying-the-shutdown-dialog-box