Skip to content

Commit

Permalink
Added CurrentDirectory to JclSysUtils.Execute
Browse files Browse the repository at this point in the history
CurrentDirectory parameter is passed to the CreateProcess
Windows function
  • Loading branch information
silverqx committed Feb 17, 2020
1 parent 3a1b168 commit 659003c
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions jcl/source/common/JclSysUtils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -500,29 +500,33 @@ function IntToStrZeroPad(Value, Count: Integer): string;

function Execute(const CommandLine: string; OutputLineCallback: TTextHandler; RawOutput: Boolean = False;
AbortPtr: PBoolean = nil; ProcessPriority: TJclProcessPriority = ppNormal;
AutoConvertOem: Boolean = False): Cardinal; overload;
AutoConvertOem: Boolean = False; const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; AbortEvent: TJclEvent;
OutputLineCallback: TTextHandler; RawOutput: Boolean = False; ProcessPriority: TJclProcessPriority = ppNormal;
AutoConvertOem: Boolean = False): Cardinal; overload;
AutoConvertOem: Boolean = False; const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; var Output: string; RawOutput: Boolean = False;
AbortPtr: PBoolean = nil; ProcessPriority: TJclProcessPriority = ppNormal;
AutoConvertOem: Boolean = False): Cardinal; overload;
AutoConvertOem: Boolean = False; const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; AbortEvent: TJclEvent;
var Output: string; RawOutput: Boolean = False; ProcessPriority: TJclProcessPriority = ppNormal;
AutoConvertOem: Boolean = False): Cardinal; overload;
AutoConvertOem: Boolean = False; const CurrentDirectory: string = ''): Cardinal; overload;

function Execute(const CommandLine: string; OutputLineCallback, ErrorLineCallback: TTextHandler;
RawOutput: Boolean = False; RawError: Boolean = False; AbortPtr: PBoolean = nil;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False): Cardinal; overload;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False;
const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; AbortEvent: TJclEvent;
OutputLineCallback, ErrorLineCallback: TTextHandler; RawOutput: Boolean = False; RawError: Boolean = False;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False): Cardinal; overload;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False;
const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; var Output, Error: string;
RawOutput: Boolean = False; RawError: Boolean = False; AbortPtr: PBoolean = nil;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False): Cardinal; overload;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False;
const CurrentDirectory: string = ''): Cardinal; overload;
function Execute(const CommandLine: string; AbortEvent: TJclEvent;
var Output, Error: string; RawOutput: Boolean = False; RawError: Boolean = False;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False): Cardinal; overload;
ProcessPriority: TJclProcessPriority = ppNormal; AutoConvertOem: Boolean = False;
const CurrentDirectory: string = ''): Cardinal; overload;

type
{$IFDEF MSWINDOWS}
Expand All @@ -549,6 +553,7 @@ function Execute(const CommandLine: string; AbortEvent: TJclEvent;
FStartupVisibility: TStartupVisibility;
FBeforeResume: TJclExecuteCmdProcessOptionBeforeResumeEvent;
{$ENDIF MSWINDOWS}
FCurrentDirectory: string;

FExitCode: Cardinal;
FOutput: string;
Expand All @@ -574,6 +579,7 @@ function Execute(const CommandLine: string; AbortEvent: TJclEvent;
property StartupVisibility: TStartupVisibility read FStartupVisibility write FStartupVisibility;
property BeforeResume: TJclExecuteCmdProcessOptionBeforeResumeEvent read FBeforeResume write FBeforeResume;
{$ENDIF MSWINDOWS}
property CurrentDirectory: string read FCurrentDirectory write FCurrentDirectory;

// out:
property ExitCode: Cardinal read FExitCode;
Expand Down Expand Up @@ -3009,6 +3015,7 @@ function ExecuteCmdProcess(Options: TJclExecuteCmdProcessOptions): Boolean;
CommandLine: string;
AbortPtr: PBoolean;
Flags: DWORD;
PCurrentDirectory: PChar;
begin
Result := False;

Expand Down Expand Up @@ -3064,6 +3071,7 @@ function ExecuteCmdProcess(Options: TJclExecuteCmdProcessOptions): Boolean;
UniqueString(CommandLine); // CommandLine must be in a writable memory block
ResetMemory(ProcessInfo, SizeOf(ProcessInfo));
ProcessEvent := nil;
PCurrentDirectory := Iff(Length(Options.CurrentDirectory) = 0, nil, PChar(Options.CurrentDirectory));
try
Flags := Options.CreateProcessFlags and not (NORMAL_PRIORITY_CLASS or IDLE_PRIORITY_CLASS or
HIGH_PRIORITY_CLASS or REALTIME_PRIORITY_CLASS);
Expand All @@ -3072,7 +3080,7 @@ function ExecuteCmdProcess(Options: TJclExecuteCmdProcessOptions): Boolean;
Flags := Flags or CREATE_SUSPENDED;

if CreateProcess(nil, PChar(CommandLine), nil, nil, True, Flags,
nil, nil, StartupInfo, ProcessInfo) then
nil, PCurrentDirectory, StartupInfo, ProcessInfo) then
begin
Result := True;
try
Expand Down Expand Up @@ -3266,7 +3274,7 @@ function ExecuteCmdProcess(Options: TJclExecuteCmdProcessOptions): Boolean;
function InternalExecute(CommandLine: string; AbortPtr: PBoolean; AbortEvent: TJclEvent;
var Output: string; OutputLineCallback: TTextHandler; RawOutput: Boolean;
MergeError: Boolean; var Error: string; ErrorLineCallback: TTextHandler; RawError: Boolean;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean; const CurrentDirectory: string): Cardinal;
var
Options: TJclExecuteCmdProcessOptions;
begin
Expand All @@ -3282,6 +3290,7 @@ function InternalExecute(CommandLine: string; AbortPtr: PBoolean; AbortEvent: TJ
Options.ErrorLineCallback := ErrorLineCallback;
Options.RawError := RawError;
Options.ProcessPriority := ProcessPriority;
Options.CurrentDirectory := CurrentDirectory;

ExecuteCmdProcess(Options);

Expand All @@ -3300,93 +3309,99 @@ function InternalExecute(CommandLine: string; AbortPtr: PBoolean; AbortEvent: TJ
That is, for RawOutput = False, lines not terminated by a line feed (#10) are deleted from Output. }

function Execute(const CommandLine: string; var Output: string; RawOutput: Boolean;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
var
Error: string;
begin
Error := '';
Result := InternalExecute(CommandLine, AbortPtr, nil, Output, nil, RawOutput, True, Error,
nil, False, ProcessPriority, AutoConvertOem);
nil, False, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

function Execute(const CommandLine: string; AbortEvent: TJclEvent; var Output: string; RawOutput: Boolean;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean; const CurrentDirectory: string): Cardinal;
var
Error: string;
begin
Error := '';
Result := InternalExecute(CommandLine, nil, AbortEvent, Output, nil, RawOutput, True, Error,
nil, False, ProcessPriority, AutoConvertOem);
nil, False, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

{ TODO -cHelp :
Author: Robert Rossmair
OutputLineCallback called once per line of output. }

function Execute(const CommandLine: string; OutputLineCallback: TTextHandler; RawOutput: Boolean;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
var
Output, Error: string;
begin
Output := '';
Error := '';
Result := InternalExecute(CommandLine, AbortPtr, nil, Output, OutputLineCallback, RawOutput, True, Error,
nil, False, ProcessPriority, AutoConvertOem);
nil, False, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

function Execute(const CommandLine: string; AbortEvent: TJclEvent; OutputLineCallback: TTextHandler; RawOutput: Boolean;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean; const CurrentDirectory: string): Cardinal;
var
Output, Error: string;
begin
Output := '';
Error := '';
Result := InternalExecute(CommandLine, nil, AbortEvent, Output, OutputLineCallback, RawOutput, True, Error,
nil, False, ProcessPriority, AutoConvertOem);
nil, False, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

{ TODO -cHelp :
RawOutput: Do not process isolated carriage returns (#13).
That is, for RawOutput = False, lines not terminated by a line feed (#10) are deleted from Output. }

function Execute(const CommandLine: string; var Output, Error: string; RawOutput, RawError: Boolean;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
begin
Result := InternalExecute(CommandLine, AbortPtr, nil, Output, nil, RawOutput, False, Error,
nil, RawError, ProcessPriority, AutoConvertOem);
nil, RawError, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

function Execute(const CommandLine: string; AbortEvent: TJclEvent; var Output, Error: string;
RawOutput, RawError: Boolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
RawOutput, RawError: Boolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
begin
Result := InternalExecute(CommandLine, nil, AbortEvent, Output, nil, RawOutput, False, Error,
nil, RawError, ProcessPriority, AutoConvertOem);
nil, RawError, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

{ TODO -cHelp :
Author: Robert Rossmair
OutputLineCallback called once per line of output. }

function Execute(const CommandLine: string; OutputLineCallback, ErrorLineCallback: TTextHandler;
RawOutput, RawError: Boolean; AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
RawOutput, RawError: Boolean; AbortPtr: PBoolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
var
Output, Error: string;
begin
Output := '';
Error := '';
Result := InternalExecute(CommandLine, AbortPtr, nil, Output, OutputLineCallback, RawOutput, False, Error,
ErrorLineCallback, RawError, ProcessPriority, AutoConvertOem);
ErrorLineCallback, RawError, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

function Execute(const CommandLine: string; AbortEvent: TJclEvent; OutputLineCallback, ErrorLineCallback: TTextHandler;
RawOutput, RawError: Boolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean): Cardinal;
RawOutput, RawError: Boolean; ProcessPriority: TJclProcessPriority; AutoConvertOem: Boolean;
const CurrentDirectory: string): Cardinal;
var
Output, Error: string;
begin
Output := '';
Error := '';
Result := InternalExecute(CommandLine, nil, AbortEvent, Output, OutputLineCallback, RawOutput, False, Error,
ErrorLineCallback, RawError, ProcessPriority, AutoConvertOem);
ErrorLineCallback, RawError, ProcessPriority, AutoConvertOem, CurrentDirectory);
end;

//=== { TJclCommandLineTool } ================================================
Expand Down

0 comments on commit 659003c

Please sign in to comment.