* Patch from Martin Friebe to implement AutoCloseCustomHandle and CustomHandleIsInheritable

This commit is contained in:
Michaël Van Canneyt 2024-02-17 10:37:13 +01:00
parent a736a4bba7
commit 5305cd2734
2 changed files with 16 additions and 4 deletions

View File

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

View File

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