[fpc-pascal] TProcess does not work in Windows XP

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Wed Mar 25 12:56:21 CET 2015


On 2015-03-24 19:56, Antonio Sanguigni wrote:
> My
> problem is Windows XP I mean, the same code seems does not produce any
> results.

I've used the following code in fpGUI's demo IDE and it works here under
FreeBSD, Linux and Win2000-Win7 (I don't have Win8 so can't test that).

The following code executes the FPC compiler an captures the results and
triggers an event, which eventually displays the output in a message window.

=============================
procedure TBuilderThread.Execute;
const
  BufSize = 1024; //4096;
var
  p: TProcess;
  c: TfpgString;
  unitdir: TfpgString;
  Buf: string;
  Count: integer;
  i: integer;
  LineStart: integer;
begin
  unitdir := GProject.ProjectDir + GProject.UnitOutputDir;
  unitdir := GMacroList.ExpandMacro(unitdir);
  if not fpgDirectoryExists(unitdir) then
  begin
    {$IFDEF DEBUG}
    writeln('DEBUG:  TBuilderThread.Execute - Creating dir: ' + unitdir);
    {$ENDIF}
    fpgForceDirectories(unitDir);
  end;

  p := TProcess.Create(nil);
  p.Options := [poUsePipes, poStdErrToOutPut];
  p.ShowWindow := swoShowNormal;
  p.CurrentDirectory := GProject.ProjectDir;

  // build compilation string
  c := gINI.ReadString(cEnvironment, 'Compiler', '');
  c := c + GProject.GenerateCmdLine(False, BuildMode);
  c := GMacroList.ExpandMacro(c);

//  AddMessage('Compile command: ' + c);
  p.CommandLine := c;
  try
    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);
          Synchronize(@DoOutputLine);
          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
      Synchronize(@DoOutputLine);
    p.WaitOnExit;
  finally
    FreeAndNil(p);
  end;

end;
=============================


Regards,
  - Graeme -

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



More information about the fpc-pascal mailing list