Pascal Scripting: ExecAndLogOutput

Prototype:

function ExecAndLogOutput(const Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ResultCode: Integer; const OnLog: TOnLog): Boolean;

Description:

Identical to Exec except:

Console programs are always hidden and the ShowCmd parameter only affects GUI programs, so always using SW_SHOWNORMAL instead of SW_HIDE is recommended.

If OnLog is set to nil then the output of the executed executable or batch file is logged in Setup's or Uninstall's log file and/or in the Compiler IDE's "Debug Output" view.

If OnLog is not set to nil then the output is sent to the specified function, line by line.

Remarks:

Parameter Wait must always be set to ewWaitUntilTerminated when calling this function.

TOnLog is defined as:

TOnLog = procedure(const S: String; const Error, FirstLine: Boolean);

Example:
var
  Line: String;

procedure ExecAndGetFirstLineLog(const S: String; const Error, FirstLine: Boolean);
begin
  if not Error and (Line = '') and (Trim(S) <> '') then
    Line := S; { First non-empty line found, store it }
  if FirstLine then
    Log('Output:');
  Log(S);
end;

function ExecAndGetFirstLine(const Filename, Params, WorkingDir: String; var ResultCode: Integer): String;
begin
  Line := '';
  ExecAndLogOutput(Filename, Params, WorkingDir, SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode, @ExecAndGetFirstLineLog);
  Result := Line;
end;
See also:

Exec