* 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;
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;

View File

@ -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);