Recent

Author Topic: Getting right ExitCode and output of TProcess  (Read 1475 times)

iyugov

  • New member
  • *
  • Posts: 8
Getting right ExitCode and output of TProcess
« on: July 16, 2018, 07:17:38 am »
I'm developing a Lazarus application which is able launch console applications, analyse their output and exit codes. Here is my run procedure:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.Button1Click(Sender: TObject);
  2.     var
  3.       Process: TProcess;
  4.       iEnvVarIndex: Integer;
  5.     begin
  6.       try
  7.         Process := TProcess.Create(Application);
  8.         try
  9.           Process.InheritHandles := False;
  10.           Process.Options := [poNewConsole, poUsePipes];
  11.           Process.ShowWindow := swoShow;
  12.           for iEnvVarIndex := 1 to GetEnvironmentVariableCount do
  13.             Process.Environment.Add(GetEnvironmentString(iEnvVarIndex));
  14.           Process.CurrentDirectory := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName)) + 'pt';
  15.           Process.Executable := './proctest_re';
  16.           Process.Execute;
  17.           repeat
  18.             // Here I'll add termination if the process runs too long
  19.           until not Process.Running;
  20.           Memo1.Lines.LoadFromStream(Process.Output);
  21.           if Process.ExitCode = 0 then
  22.             ShowMessage('OK')
  23.           else
  24.             ShowMessage('Runtime error.');
  25.         finally
  26.           Process.Free;
  27.         end;
  28.       except
  29.         on Exception do
  30.           ShowMessage('Could not start');
  31.       end;
  32.   end;
  33.  
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
« Last Edit: July 16, 2018, 07:39:44 am by iyugov »

 

TinyPortal © 2005-2018