added TProcess utils to ce_common

This commit is contained in:
Basile Burg 2014-11-07 14:06:44 +01:00
parent 83b568904c
commit dd65981470
1 changed files with 47 additions and 1 deletions

View File

@ -9,7 +9,7 @@ uses
{$IFDEF WINDOWS}
Windows,
{$ENDIF}
ActnList, dialogs, forms, process;
ActnList, dialogs, forms, process, asyncprocess;
const
DdiagFilter = 'D source|*.d|D interface|*.di|All files|*.*';
@ -179,6 +179,16 @@ type
*)
function exeInSysPath(anExeName: string): boolean;
(**
* Clears then fills aList with aProcess output stream.
*)
procedure processOutputToStrings(const aProcess: TProcess; var aList: TStringList);
(**
* Terminates and frees aProcess;
*)
procedure killProcess(var aProcess: TAsyncProcess);
implementation
// https://stackoverflow.com/questions/25438091/objectbinarytotext-error-with-a-treader-twriter-helper-class
@ -666,6 +676,42 @@ begin
exit(ExeSearch(anExeName, '') <> '');
end;
procedure processOutputToStrings(const aProcess: TProcess; var aList: TStringList);
var
str: TMemoryStream;
sum: Integer;
cnt: Integer;
const
buffSz = 1024;
begin
if not (poUsePipes in aProcess.Options) then
exit;
//
sum := 0;
str := TMemoryStream.Create;
try
while aProcess.Output.NumBytesAvailable <> 0 do begin
str.Size := str.Size + buffSz;
cnt := aProcess.Output.Read((str.Memory + sum)^, buffSz);
sum += cnt;
end;
str.Size := sum;
aList.LoadFromStream(str);
finally
str.Free;
end;
end;
procedure killProcess(var aProcess: TAsyncProcess);
begin
if aProcess = nil then
exit;
if aProcess.Running then
aProcess.Terminate(0);
aProcess.Free;
aProcess := nil;
end;
initialization
RegisterClasses([TMRUList, TMRUFileList]);
DExtList := TStringList.Create;