Преглед изворни кода

* Patch from Martin Friebe to implement AutoCloseCustomHandle and CustomHandleIsInheritable

Michaël Van Canneyt пре 1 година
родитељ
комит
5305cd2734
2 измењених фајлова са 16 додато и 4 уклоњено
  1. 7 0
      packages/fcl-process/src/processbody.inc
  2. 9 4
      packages/fcl-process/src/win/process.inc

+ 7 - 0
packages/fcl-process/src/processbody.inc

@@ -49,6 +49,8 @@ Type
      FAfterAllocateHandle: TAfterAllocateHandleEvent;
      FCloseHandleOnExecute: Boolean;
      FCustomHandle: THandle;
+     FAutoCloseCustomHandle: Boolean;
+     FCustomHandleIsInheritable: Boolean;
      FFileWriteMode: TFileWriteMode;
      FHandleType: TProcessHandleType;
      FFileName: TFileName;
@@ -97,6 +99,8 @@ Type
      Destructor Destroy; override;
      Property ProcessHandleType : TProcessHandleType Read FHandleType;
      Property CustomHandle : THandle Read FCustomHandle Write FCustomHandle;
+     Property AutoCloseCustomHandle: Boolean Read FAutoCloseCustomHandle Write FAutoCloseCustomHandle;
+     Property CustomHandleIsInheritable: Boolean Read FCustomHandleIsInheritable Write FCustomHandleIsInheritable; platform;
 
    Published
      Property IOType : TIOType Read FIOType Write SetIOType;
@@ -1181,6 +1185,8 @@ begin
     FTheirHandleIOType := IOType;
     FOurHandle:=THAndle(INVALID_HANDLE_VALUE);
     FCloseHandleOnExecute:=(IOType<>iotDefault);
+    if IOType = iotHandle then
+      FCloseHandleOnExecute:=FAutoCloseCustomHandle;
     Case IOType of
       iotDefault : H:=CreateStandardHandle;
       iotPipe : H:=CreatePipeHandle;
@@ -1205,6 +1211,7 @@ begin
   FTheirHandle:=THandle(INVALID_HANDLE_VALUE);
   FOurHandle:=THandle(INVALID_HANDLE_VALUE);
   FPipeBufferSize := 1024;
+  FAutoCloseCustomHandle := True;
 end;
 
 destructor TIODescriptor.Destroy;

+ 9 - 4
packages/fcl-process/src/win/process.inc

@@ -353,10 +353,11 @@ var
   Res : Boolean;
   
 begin
-  if IOType in [iotDefault,iotFile] then begin
+  if (IOType in [iotDefault,iotFile]) or ((IOType=iotHandle) and FCustomHandleIsInheritable) then
+    begin
     Result:=aHandle;
     exit;
-  end;
+    end;
   oldHandle := ahandle;
   ahandle:=THandle(INVALID_HANDLE_VALUE); 
   Res := DuplicateHandle
@@ -368,8 +369,12 @@ begin
     true,
     DUPLICATE_SAME_ACCESS
   );
-  if Res then
-    Res:=CloseHandle(oldHandle);
+  if Res then begin
+    if (IOType=iotHandle) and not FAutoCloseCustomHandle then
+      FCloseHandleOnExecute:=True // AutoCloseCustomHandle protects the original Handle
+    else
+      Res:=CloseHandle(oldHandle);
+  end;
   if not Res then
     begin
     FileClose(aHandle);