[fpc-pascal] TProcess usage and reading program output

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Tue Feb 28 17:06:43 CET 2017


Hi,

Can anybody see if there is something wrong with the code shown below.
The code is copied from one of my earlier projects where I call the FPC
compiler and it worked just fine in that project.

In the work I'm doing now, I'm calling the Delphi Command Line Compiler,
and made a few minor tweaks to the code, but this method never seems to
return when Delphi command line compiler is called, and I don't see any
output. If I call other utilities (eg: Git etc) then I do see output and
it works as expected. It's just the Delphi Command Line Compiler that
now seems to be giving troubles.

I would really appreciate it is anybody could spare a moment. Many thanks.

I marked two areas with "// ???" where I am unsure if I'm doing the
right thing.


==================================
function TBuildDelphiProject.RunTProcess(const Binary: string; args:
TStrings): boolean;
const
  BufSize = 1024;
var
  p: TProcess;
  Buf: string;
  Count: integer;
  i: integer;
  LineStart: integer;
  OutputLine: string;
begin
  p := TProcess.Create(nil);
  try
    p.Executable := Binary;
    // ??? Is poWaitOnExit needed here, it is called later down the code
    p.Options := [poUsePipes, poStdErrToOutPut, poWaitOnExit];
//    p.CurrentDirectory := ExtractFilePath(p.Executable);
    p.ShowWindow := swoShowNormal;  // ??? Is this needed?

    p.Parameters.Assign(args);
    DoLog(etInfo,'Running command "%s" with arguments
"%s"',[p.Executable, p.Parameters.Text]);
    p.Execute;

    { Now process the output }
    OutputLine:='';
    SetLength(Buf,BufSize);
    repeat
      if (p.Output<>nil) then
      begin
        Count:=p.Output.Read(Buf[1],Length(Buf));
      end
      else
        Count:=0;
      LineStart:=1;
      i:=1;
      while i<=Count do
      begin
        if Buf[i] in [#10,#13] then
        begin
          OutputLine:=OutputLine+Copy(Buf,LineStart,i-LineStart);
          writeln(OutputLine);
          OutputLine:='';
          if (i<Count) and (Buf[i+1] in [#10,#13]) and
(Buf[i]<>Buf[i+1]) then
            inc(i);
          LineStart:=i+1;
        end;
        inc(i);
      end;
      OutputLine:=Copy(Buf,LineStart,Count-LineStart+1);
    until Count=0;
    if OutputLine <> '' then
      writeln(OutputLine);
    p.WaitOnExit;
    Result := p.ExitStatus = 0;
    if Not Result then
      Writeln('Command ', p.Executable ,' failed with exit code: ',
p.ExitStatus);
  finally
    FreeAndNil(p);
  end;
end;
==================================


Regards,
  Graeme

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp



More information about the fpc-pascal mailing list