Not sure why, but sometimes the Delphi IDE does not allow you to toggle the Console Application; it looks off, but is in fact on: IsConsole returns true
Posted by jpluimers on 2019/03/28
On my research list: Not sure why, but sometimes the Delphi options display a regular application, but the IsConsole returns true because of AppType Console in the main PropertyGroup.
- console applications are turned off in the linker
- the debugger still shows it as a console application when stepping through the
initialization
section of theSystem
unit
One of the problems is that unlike the {$APPTYPE
directive (which has not changed much over time), the AppType
element is undocumented (hardly anything in the .dproj file is documented).
The $APPTYPE
lists two values: Console
and GUI
, defaulting to GUI
:
- [WayBack] Application type (Delphi 2007)
- [WayBack] Application type (Delphi 2009)
- [WayBack] Application type (Delphi 10.2 Tokyo) – RAD Studio
Empirically, the AppType
element can have these values:
Application
Console
Package
This is the topmost part of the .dproj file does not matter, without AppType
element valued Console
, or with it or even having it valued Application
: either way a console application is being built.
There is no $APPTYPE
in the .dpr
or any of the .pas
files.
The IsConsole
variable is always false and a console window always appears when debugging.
- [WayBack] System.IsConsole Variable (Delphi 2007)
- [WayBack] System.IsConsole Variable (Delphi 2009)
- [WayBack] System.IsConsole – RAD Studio API Documentation (Delphi 10.2 Tokyo)
That variable is not always conclusive, as there are situations where code is inside a BPL or DLL started by an EXE, so this works better: [WayBack] How to determine if I’m running as a console app? (Delphi on Win32) – Stack Overflow:
function IAmAConsoleApp: Boolean; var Stdout: THandle; begin Stdout := GetStdHandle(Std_Output_Handle); Win32Check(Stdout <> Invalid_Handle_Value); Result := Stdout <> 0; end;
The .dproj files:
https://gist.github.com/jpluimers/a2839f02857e2a9aaf719098a3630a0c
–jeroen
Remy said
The GetStdHandle() trick will not work if your app is launched with a redirected STDOUT. The returned handle may be a valid non-console handle. You may need to use GetFileType() to check what TYPE of handle it is.
dummzeuch said
Not sure if this helps any, but I blogged about console applictions in Dephi in 2014: