Recent

Author Topic: Redirecting Std I/O in GUI mode  (Read 766 times)

Red_prig

  • Full Member
  • ***
  • Posts: 158
Redirecting Std I/O in GUI mode
« on: June 23, 2026, 07:09:35 pm »
This may have been discussed many times, but Windows Target has an annoying quirk where, in GUI mode, writing to the console via Write/Writeln throws a runtime exception. Today, this code snippet occurred to me:

Code: Pascal  [Select][+][-]
  1. uses
  2.  windows,
  3.  sysutils;
  4.  
  5. Procedure InitStdIo;
  6. var
  7.  F:THandle;
  8. begin
  9.  if AttachConsole(ATTACH_PARENT_PROCESS) then
  10.  begin
  11.   //
  12.  end else
  13.  begin
  14.   //redirect to NULL
  15.   F:=FileOpen('NUL',fmOpenReadWrite);
  16.   StdInputHandle :=F;
  17.   StdOutputHandle:=F;
  18.   StdErrorHandle :=F;
  19.   SetStdHandle(STD_INPUT_HANDLE ,F);
  20.   SetStdHandle(STD_OUTPUT_HANDLE,F);
  21.   SetStdHandle(STD_ERROR_HANDLE ,F);
  22.  end;
  23.  
  24.  //mark console I/O
  25.  IsConsole:=True;
  26.  
  27.  //reinit std I/O
  28.  SysInitStdIO;
  29. end;
  30.  

It literally connects to the console if the application is running through it or doesn't throw an error, which is similar to Linux behavior, and allows for a unique way to capture logs from graphical applications in specific cases.

I don't know what the FPC command's policy is on this, but wouldn't it be better to implement this in a system module?

PascalDragon

  • Hero Member
  • *****
  • Posts: 6403
  • Compiler Developer
Re: Redirecting Std I/O in GUI mode
« Reply #1 on: June 23, 2026, 08:53:03 pm »
I don't know what the FPC command's policy is on this, but wouldn't it be better to implement this in a system module?

No, because the correct policy on Windows is for GUI applications not to attach to the console. The application will after all not block the console and thus the user might want to use it for something else. In fact I'd throw such an application from my system if it would dare to do such nonsense. Windows is not Unx.

Red_prig

  • Full Member
  • ***
  • Posts: 158
Re: Redirecting Std I/O in GUI mode
« Reply #2 on: June 23, 2026, 09:36:54 pm »
Well, I'm writing an emulator and I have to take logs in difficult cases, when the GUI breaks for some reason (for example, because of an incorrectly programmed VEH), or, for example, use a dynamic config redirection of stdout either to a real console, or to a file, or somewhere else

Red_prig

  • Full Member
  • ***
  • Posts: 158
Re: Redirecting Std I/O in GUI mode
« Reply #3 on: June 23, 2026, 09:42:22 pm »
There are also cases where something breaks during the initialization stage before the GUI launches, and I need to debug it on Discord with another user, kekw

mas steindorff

  • Hero Member
  • *****
  • Posts: 603
Re: Redirecting Std I/O in GUI mode
« Reply #4 on: June 23, 2026, 10:42:09 pm »
I used LazLogger.  it can write debug info to an actual file or console output.  it was robust back when I needed it but that has been a few years.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

LeP

  • Sr. Member
  • ****
  • Posts: 445
Re: Redirecting Std I/O in GUI mode
« Reply #5 on: June 23, 2026, 11:03:31 pm »
I don't know what the FPC command's policy is on this, but wouldn't it be better to implement this in a system module?
No, because the correct policy on Windows is for GUI applications not to attach to the console. The application will after all not block the console and thus the user might want to use it for something else. In fact I'd throw such an application from my system if it would dare to do such nonsense. Windows is not Unx.

In Delphi you can use simple this (are simple Windows apis, and works):

Code: Pascal  [Select][+][-]
  1.       if GetConsoleWindow = 0 then
  2.         begin
  3.           AllocConsole;
  4.         end;
  5.  

With this, writeln is working, I use that for debugging purpose, very easy 'cause you can activate that at runtime when needed.
But in Lazarus it doesn't work.

Of course, like in Lazarus you can setup a console working in the project option too, but this is permanent.

Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

Red_prig

  • Full Member
  • ***
  • Posts: 158
Re: Redirecting Std I/O in GUI mode
« Reply #6 on: June 23, 2026, 11:29:44 pm »
I don't know what the FPC command's policy is on this, but wouldn't it be better to implement this in a system module?
No, because the correct policy on Windows is for GUI applications not to attach to the console. The application will after all not block the console and thus the user might want to use it for something else. In fact I'd throw such an application from my system if it would dare to do such nonsense. Windows is not Unx.

In Delphi you can use simple this (are simple Windows apis, and works):

Code: Pascal  [Select][+][-]
  1.       if GetConsoleWindow = 0 then
  2.         begin
  3.           AllocConsole;
  4.         end;
  5.  

With this, writeln is working, I use that for debugging purpose, very easy 'cause you can activate that at runtime when needed.
But in Lazarus it doesn't work.

Of course, like in Lazarus you can setup a console working in the project option too, but this is permanent.

You can do this, but digging into the source code for this is still useful, albeit tedious.

Code: Pascal  [Select][+][-]
  1. if GetConsoleWindow = 0 then
  2.   begin
  3.     AllocConsole;
  4.     IsConsole:=True;
  5.     SysInitStdIO;
  6.   end;
  7.  

440bx

  • Hero Member
  • *****
  • Posts: 6560
Re: Redirecting Std I/O in GUI mode
« Reply #7 on: June 23, 2026, 11:47:11 pm »
I ran into this FPC console problem a number of years ago.  It might be worth mentioning that Delphi does not suffer from that problem.

The discussion of the problem is found at:
https://forum.lazarus.freepascal.org/index.php/topic,44828.0.html
and the last post includes an attachment with an example program that works under both, Delphi and FPC
https://forum.lazarus.freepascal.org/index.php/topic,44828.msg315571.html#msg315571

Just FYI, what is found in that thread is pretty much what has already been discussed in this thread, though there may be one or two additional details there that are not present in this thread.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

LeP

  • Sr. Member
  • ****
  • Posts: 445
Re: Redirecting Std I/O in GUI mode
« Reply #8 on: June 24, 2026, 09:17:17 am »
and the last post includes an attachment with an example program that works under both, Delphi and FPC

I look in the code, but I didn't try it, and I think that it doesn't work in the modern edition of Delphi. You cannot recreate a console after destroying it (writeln will not work). In Delphi (like in FPC) maybe some more code is needed.

I have never looked into the subject in depth.
Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

440bx

  • Hero Member
  • *****
  • Posts: 6560
Re: Redirecting Std I/O in GUI mode
« Reply #9 on: June 24, 2026, 11:26:46 am »
and the last post includes an attachment with an example program that works under both, Delphi and FPC

I look in the code, but I didn't try it, and I think that it doesn't work in the modern edition of Delphi. You cannot recreate a console after destroying it (writeln will not work). In Delphi (like in FPC) maybe some more code is needed.

I have never looked into the subject in depth.
I've used it in Seattle which is not that old and there is no problem creating and destroying consoles multiple times.  I suggest you try the code, that way you'll know facts instead of speculating.
« Last Edit: June 24, 2026, 11:33:35 am by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

LeP

  • Sr. Member
  • ****
  • Posts: 445
Re: Redirecting Std I/O in GUI mode
« Reply #10 on: June 24, 2026, 11:59:52 am »
I've used it in Seattle which is not that old and there is no problem creating and destroying consoles multiple times.  I suggest you try the code, that way you'll know facts instead of speculating.

Seattle is more old then 1 decade ago ... if you think it is modern ... speculating on words is your hobby.

My post is about signaling that in "new versions" (it's more understandable what I'm referring to ?) of Delphi the code proposed maybe is not working (my think). I thought it would be useful for the community, but evidently I was wrong, different experiences are not welcome.
I don't need to test and don't want to lost time (it's your code), anyone who is interested can test and see.

N.B.: if someone tell me that MY code is not working, I take the code and try it ... after that one can spoke about speculation.

I stop this discussion here.
Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12959
  • FPC developer.
Re: Redirecting Std I/O in GUI mode
« Reply #11 on: June 24, 2026, 12:24:51 pm »
(Afaik Seattle is the newest Delphi that can be bought(perpetual license). The versions after it are only leased (subscription), I can be wrong though, that some of the later 10.x versions are also perpetual, and only the initial purchase required taking one year subscription, but not to keep running it)

Anyway, the differences might be related to the input/output/error being threadvars in FPC, that also might complicate calling such internal routines. I don't know if Delphi also does that. So calling winapi functions to reenable it uses undocumented behaviour of the Delphi RTL.



« Last Edit: June 24, 2026, 02:03:35 pm by marcov »

gidesa

  • Sr. Member
  • ****
  • Posts: 260
Re: Redirecting Std I/O in GUI mode
« Reply #12 on: June 24, 2026, 01:26:24 pm »
Add:
{$APPTYPE CONSOLE}
Works in Objfpc and Delphi mode.
Then you have a console, can write, writeln.

Red_prig

  • Full Member
  • ***
  • Posts: 158
Re: Redirecting Std I/O in GUI mode
« Reply #13 on: June 24, 2026, 02:41:15 pm »
Add:
{$APPTYPE CONSOLE}
Works in Objfpc and Delphi mode.
Then you have a console, can write, writeln.

My main idea was to make console output optional at runtime, but the problem with {$APPTYPE CONSOLE}
is that it creates a console window if there isn't one.

gidesa

  • Sr. Member
  • ****
  • Posts: 260
Re: Redirecting Std I/O in GUI mode
« Reply #14 on: June 24, 2026, 06:45:43 pm »
My main idea was to make console output optional at runtime, but the problem with {$APPTYPE CONSOLE}
is that it creates a console window if there isn't one.

The idea is to use Apptype console when you debug a Gui program. 
Maybe there is a mode to get the console handle generated
with this option, and then hide it, or do anything you want.

 

TinyPortal © 2005-2018