The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

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.

  1. console applications are turned off in the linker
  2. the debugger still shows it as a console application when stepping through the initialization section of the System 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:

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.

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

2 Responses to “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”

  1. 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.

  2. dummzeuch said

    Not sure if this helps any, but I blogged about console applictions in Dephi in 2014:

    Delphi Console Applications

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.