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.

If your Delphi version is newer than 4, it's very easy to set up a new console application with the Console Application Wizard:

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

2. Select the Console Application icon and click OK.

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

program Project1;
{$APPTYPE CONSOLE}
uses SysUtils;

begin
  // Insert user code here
end.

If your Delphi version is 4 or earlier, you have to do a bit more manual work to create an empty console project:

  1. Click menu option File / New Application.
  2. Click menu optionProject / Remove From Project...
  3. Select Unit1 and click OK. Delphi will remove the Unit1 from your project.
  4. Click menu option Project / View Source in order to open the project file.
  5. In the Code Editor, delete all the text and replace it with the text of the program template above:
    "program Project1" and so on...

A simple example

  1. Start a new console application, using one of the two methods described above.
  2. Save the project as ConsoleTest in an empty folder of your choice. The project file will be saved as ConsoleTest.dpr.
  3. Add 3 lines of text between the keywords begin and end:
    program ConsoleTest;
     
    {$APPTYPE CONSOLE}
    
    uses SysUtils;
    
    begin
      WriteLn('Program ConsoleTest is running.');
      WriteLn('Press the ENTER key to stop');
      ReadLn;
    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

TOP :: Source Code and Tutorials :: Crash Course Delphi :: Forum DelphiLand Club
DC Library :: Tips :: Downloads :: Links

© Copyright 1999-2016 
Studiebureau Festraets