I'm developing a Lazarus application which is able launch console applications, analyse their output and exit codes. Here is my run procedure:
procedure TfrmMain.Button1Click(Sender: TObject);
var
Process: TProcess;
iEnvVarIndex: Integer;
begin
try
Process := TProcess.Create(Application);
try
Process.InheritHandles := False;
Process.Options := [poNewConsole, poUsePipes];
Process.ShowWindow := swoShow;
for iEnvVarIndex := 1 to GetEnvironmentVariableCount do
Process.Environment.Add(GetEnvironmentString(iEnvVarIndex));
Process.CurrentDirectory := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName)) + 'pt';
Process.Executable := './proctest_re';
Process.Execute;
repeat
// Here I'll add termination if the process runs too long
until not Process.Running;
Memo1.Lines.LoadFromStream(Process.Output);
if Process.ExitCode = 0 then
ShowMessage('OK')
else
ShowMessage('Runtime error.');
finally
Process.Free;
end;
except
on Exception do
ShowMessage('Could not start');
end;
end;
In the application directory I have a 'pt' directory which contains two executables written in FreePascal:
1) proctest_ok (begin Write('OK'); end.);
2) proctest_re (begin Write('ERROR'); Halt(1); end.).
No matter what I specify in the Process.Executable (proctest_ok, proctest_re, proctest_nonexistent), I get an 'OK' (ExitCode = 0) and empty Memo1. Same problem with ExitStatus.
UPD: proctest_ok and proctest_re are actually launched. I made them create text files, and they did it on the button code above.
How can my application know if the executable was really launched?
How can I get true ExitCode and output of the process? (assuming the output is always small)
Lazarus v1.8.2+dfsg3, Ubuntu 18.04