[fpc-devel] Free Pascal / Free Vision: Executing shell commands and showing output
Michael Van Canneyt
michael at freepascal.org
Wed Jan 28 08:45:15 CET 2026
On Tue, 27 Jan 2026, Bart via fpc-devel wrote:
> On Tue, Jan 27, 2026 at 9:44 AM Aruna Hewapathirane via fpc-devel
> <fpc-devel at lists.freepascal.org> wrote:
>
>
>> The attached screenshot shows the issue I’m running into with the displayed output. I’m clearly missing something fundamental here, and I’d appreciate any pointers in the right direction.
>
> What happens if you output the Result to a file?
>
> I use similar code (allbeit in a Lazarus program), I use
> LoadFromStream(Proc.Output) instead of your CopyFrom(), not sure if
> that matters.
It does.
1. Do not use CopyFrom on a non-seekable stream such as a pipe (which is used to capture output in TProcess).
this is documented: https://www.freepascal.org/docs-html/rtl/classes/tstream.copyfrom.html
2. Do not use the poWaitOnExit flag. When the program you start outputs more
data then the size of the OS pipe buffer then it will be blocked waiting for
you to free the pipe buffer, while you wait for it to finish.
Instead, do a loop:
...
Proc.Options := Proc.Options + [poUsePipes]-[poWaitOnExit];
Proc.Execute;
While Proc.Running do
begin
nBytesRead:=Proc.Output.Read(Buffer,SizeOf(Buffer);
S.WriteBuffer(Buffer,BytesRead);
end;
// Read remaining
nBytesRead:=proc.Output.Read(Buffer,SizeOf(Buffer);
S.WriteBuffer(Buffer,BytesRead);
// ...
// Check exit status and further processing
Michael.
More information about the fpc-devel
mailing list