Delphi Tutorials »

Console Applications, part 1: Setting it up and Compiling

What is a Delphi Console Application?

While originally Delphi was developed for writing Windows applications with a "Graphical User Interface" (GUI), you can also use it for Console Applications. Worldwide, teachers use these pure text programs for teaching Pascal. Hey, back to the pre-historic DOS days!

When you start a console application, Windows creates a text-mode console window through which the user can interact with the application.

Note that a console program is not a DOS program, because it can call Windows functions.


Setting up a simple console application

The main source code for a console application is contained in a textfile with a .DPR extension, a Delphi Project file. If desired, you can add additional source code units, .PAS files, but for simple programs this is not necessary.

Console applications don't use the visual controls of Delphi's Visual Controls Library (VCL). So, since there are no "forms", there are no .DFM files.

In Delphi Community Edition, it's very easy to set up a new console application:

1. Select File / New / Other... This opens the New Items dialog window.
(Note that in some older Delphi versions, the New Items dialog is directly opened with File / New)

2. Select Console Application and click OK.

In the code editor, you now see a program template:

program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
  System.SysUtils;

begin
  try
    { TOTO -oUser -cConsole Main :  Insert code here }
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

A simple example

  1. Start a new console application.
  2. Save the project as ConsoleTest in an empty folder of your choice, such as: C:\ConsAp
    The project file will be saved as ConsoleTest.dpr.
  3. Add 3 lines under the line with "try":
    begin
      try
        WriteLn('Program ConsoleTest is running.');
        WriteLn('Press the ENTER key to stop');
        ReadLn;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.
  4. To compile and run your program, simply press F9 (or select menu Run / Run).
    Because of the line {$APPTYPE CONSOLE}, Delphi compiles the program as a console application and afterwards runs it in a console window.  
  5. Check how your program runs as a stand-alone application:

    - in your favorite file manager (for example Windows' Explorer;) double click the file ConsoleTest.exe

    - or use the "Run..." feature in Window's Start menu, and enter the command:
    c:\ConsAp\ConsoleTest

    - or open a DOS-window ("go to the command-prompt"), navigate to the directory \ConsAp, type ConsoleTest and press ENTER.

» Part 2: Structure of the program; Input and Output
» Part 3: Program loop

 




Source Code and Tutorials :: Crash Course Delphi :: FAQ
DC Library :: Tips :: Downloads :: Links

© Copyright 1999-2019 
Studiebureau Festraets