Recent

Author Topic: Apptype Gui or Console - decide from code  (Read 1284 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Apptype Gui or Console - decide from code
« Reply #15 on: November 24, 2025, 11:23:56 am »
Yes, correct. Anyway simply use GetConsoleWindow<> 0; to get it manually. It only has value on Windows anyway.
On UNIX's it is always true.
For Windows, that is the only correct way: no tricks, pure API.

It is also very common in Windows programming that an GUI application accepts parameters when started from a console.
It is just Delphi legacy that a GUI app has no input/output/erroutput by default. Nothing to do with Windows.
All that's required is to install the console stuff before Application.Run in the project file. Hidden or not.
E.g:
Code: Text  [Select][+][-]
  1. notepad.exe newfile.txt
Code: Text  [Select][+][-]
  1. msedge.exe --new-window
Etc, etc...
« Last Edit: November 24, 2025, 01:53:09 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

tetrastes

  • Hero Member
  • *****
  • Posts: 739
Re: Apptype Gui or Console - decide from code
« Reply #16 on: November 24, 2025, 02:37:58 pm »
https://www.freepascal.org/docs-html/rtl/system/isconsole.html
I'm guessing that since he wants his program to decide whether it is a console of gui program he has to set the variable accordingly.  IOW, he is attempting to override what the compiler knows the program to be.

 :D

To OP: the type of Widows app is defined in PE header, and I doubt that setting a variable at run time can change it.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Apptype Gui or Console - decide from code
« Reply #17 on: November 24, 2025, 03:06:39 pm »
https://www.freepascal.org/docs-html/rtl/system/isconsole.html
I'm guessing that since he wants his program to decide whether it is a console of gui program he has to set the variable accordingly.  IOW, he is attempting to override what the compiler knows the program to be.

 :D

To OP: the type of Widows app is defined in PE header, and I doubt that setting a variable at run time can change it.
That is not the place where Windows looks: that is GetConsoleWindow. A simple example is to allocate the console on the first line of the lpr.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

440bx

  • Hero Member
  • *****
  • Posts: 6086
Re: Apptype Gui or Console - decide from code
« Reply #18 on: November 24, 2025, 03:27:10 pm »
It is just Delphi legacy that a GUI app has no input/output/erroutput by default. Nothing to do with Windows.
It has plenty to do with Windows.  Windows will _not_ set stdin, stdout and stderr for GUI apps. 

Therefore, in Windows, for a GUI app, it is not possible to redirect standard input, output and error. 

Enabling a GUI app to use stdin, stdout and/or stderr is no picnic.  Actually, it is so hard that there isn't even a single working example of something like:
Quote
c:\> myguiapp < inputfile > outputfile
I have the only program that makes that work (written in Pascal (FPC) ... and I'm keeping it to myself. :) )  I won't even post the executable because it's way too easy to reverse engineer.   

or even better:

Quote
c:\> myguiapp < inputfile | notepad.exe
That one, I couldn't make it work (wasn't for lack of trying!)



That is not the place where Windows looks: that is GetConsoleWindow. A simple example is to allocate the console on the first line of the lpr.
It is where Windows looks, specifically the loader (Ldr functions in ntdll).  It looks in the PE header to determine what to do.  The setting of the IMAGE subsystem is what determines what the Windows loader will do as far as creating a GUI, CONSOLE or NATIVE app.  What the loader does is _very_ different depending on the type of app (though there is a lot of code that is common.)

GetConsoleWindow() is just a run-of-the-mill API that has no bearing on the type of app, no more than BeginPaint or GetClientRect do.  For the record, AllocateConsole will, as its name implies, allocate a console but it most definitely does not turn a GUI app into a console app (not even close.)
« Last Edit: November 24, 2025, 03:29:46 pm by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018