[fpc-devel] StdOut capture for FPC RTL

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Nov 25 18:57:36 CET 2010


On 25 Nov 2010, at 13:21, Jonas Maebe wrote:

> 
> On 25 Nov 2010, at 12:24, Anton Kavalenka wrote:
> 
>> What I have to do to properly initialize these defaults for new threads AFTER capturing StdOut?
> 
> Store a copy of your stdout in a global variable, and after creating a new thread
> 
>  close(stdout);
>  stdout:=myglobalstdout;
> 
> (and maybe the same for "output").

Actually, that won't work because the different threads will then work on a common buffer but with distinct pointers into it. A better solution is probably to do this in the intialisation code of each thread instead:

{$ifdef unix}
  fpclose(ttextrec(stdout).handle);
{$elsif defined(MSWINDOWS)}
  { this is a copy of do_close() from the rtl, I don't know whether
    a new handle from a thread can actually have any of these values }
  if (handle <> StdInputHandle) and
     (handle <> StdOutputHandle) and
     (handle <> StdErrorHandle) then
    CloseHandle(ttextrec(stdout).handle);
{$else}
  {$error Add support for this platform}
{$endif}

ttextrec(stdout).handle:=myglobalstdouthandle;


Jonas


More information about the fpc-devel mailing list