Delphi Console Applications

 Delphi  Comments Off on Delphi Console Applications
Apr 102014

Delphi allows to write Console applications as well as GUI applications, while the latter is the default.

But “Console” application is not clearly defined in the documentation as I found out only today (and I have been programming in Delphi for about 20 years).

There is the {$AppType Console} compiler directive which corresponds to the /cc compiler switch and which has no corresponding setting in the compiler options dialog. The File -> New -> Console Application menu generates a project containing this directive. As documented in the Delphi online help this compiler directive opens the INPUT and OUTPUT files which are associated with the stdin and stdout handles. In addition, at least since Delphi 2007 it also opens the ErrOutput file which is associated with the stderr handle. (The latter doesn’t seem to be documented.) Also, the IsConsole function returns true for programs compiled with this directive. The conditional symbol CONSOLE is not defined for these programs unless you also set the linker option "Generate console application". (Which is odd. Why should a linker option have any effect on a conditional symbol for the compiler? But that’s how it is.)

Then, there is the linker option "Generate console application". This option is not automatically set if you use the File -> New -> Console application menu. If you set it, the conditional symbol CONSOLE is defined. You can also set this option for a GUI program. In this case, CONSOLE also defined and in addition to your program’s form, you will also get a console window. Also IsConsole returns true and the files INPUT, OUTPUT and ErrOutput are opened.

So there are four possible combinations:

  1. AppType CONSOLE + "Generate console application" is set -> CONSOLE is defined and IsConsole returns true
  2. AppType GUI + "Generate console application" is set -> CONSOLE is defined and IsConsole returns true
  3. AppType CONSOLE + "Generate console application" is not set -> CONSOLE is not defined and IsConsole returns true
  4. AppType GUI + "Generate console application" is not set -> CONSOLE is not defined and IsConsole returns false

I could not find any difference between the cases 1 and 2. Maybe some additional startup code is generated in one of these cases? But the executable size doesn’t seem to change either. So, what is the actual point of the compiler directive?

So the important lesson here is:

The files INPUT, OUTPUT and ErrOutput are opened automatically if IsConsole returns true, regardless of the conditional symbol CONSOLE. I don’t really see the use of the latter.

For the curious here is the program code I used to find out the above:

program ConsoleTest;



  WriteLn('CONSOLE is defined');
  if IsConsole then
    WriteLn('CONSOLE is not defined')
    Windows.MessageBox(0, 'CONSOLE is not defined', nil, MB_OK or MB_ICONINFORMATION);
  if IsConsole then begin
    WriteLn('IsConsole returns true');
    WriteLn('Press Enter');
  end else begin
    Windows.MessageBox(0, 'IsConsole returns false', nil, MB_OK or MB_ICONINFORMATION);

 Posted by on 2014-04-10 at 10:55

Creating DLLs with Delphi

 Delphi  Comments Off on Creating DLLs with Delphi
Apr 102014

I just stumbled upon an article on The Delphi Wiki that I wrote years ago:

Creating DLLs

It’s still mostly valid. There have been some improvements in newer Delphi versions, in particular since Delphi 2006 you no longer need the borlndmm.dll if you want to use ShareMem. (But still: Using ShareMem means that your DLL can only be used by Delphi programs, so don’t do that, if you can avoid it.)

That’s not the only article I wrote for that Wiki. It’s interesting to see how much time I apparently had on my hands back then. Sometimes I wish those “good times” came back.

 Posted by on 2014-04-10 at 09:27