Pascal Scripting: ExecAndCaptureOutput

Prototype:

function ExecAndCaptureOutput(const Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ResultCode: Integer; var Output: TExecOutput): Boolean;

Description:

Identical to Exec except:

Program output from the stdout and stderr streams is captured. This is different from ExecAndLogOutput which merges the streams.

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

An exception will be raised if there was an error setting up output redirection (which should be very rare).

Remarks:

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

TExecOutput is defined as:

  TExecOutput = record
    StdOut: TArrayOfString;
    StdErr: TArrayOfString;
    Error: Boolean;
  end;

Error will be True if there was an error reading the output (which should be very rare) or if the output is too big. The error is logged in Setup's or Uninstall's log file and/or in the Compiler IDE's "Debug Output" view. There is no further output after an error.

Output is limited to a total size of 10 million bytes or a maximum of 1 million lines.

Example:
var
  ResultCode: Integer;
  Output: TExecOutput;
begin
  try
    // Get the system configuration
    Result := ExecAndCaptureOutput(ExpandConstant('{cmd}'), '/c systeminfo', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode, Output);
  except
    Result := False;
    Log(GetExceptionMessage);
  end;

  if Result then begin
    // Do something with the Output.StdOut array of string
  end;
end;
See also:

Exec
ExecAndLogOutput