IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Basics   Print This     
  From the October 2015 Issue of Prestwood eMag
 
Delphi Language Basics:
A 10 Minute Delphi Console App Quick Start
 
Posted 11 years ago on 10/8/2008
Take Away:

Create a classic "Hello, World" Windows native code Console App using Delphi. This tutorial is based on Borland Developer Suite 2006 but you can use any version of Delphi you wish.

KB101326



Got 10 minutes? Want to get started with Delphi Console App?

This article is part of our series of 10 Minute Quick Starts. Each quick start is step by step, assumes you know very little about the subject, and takes about 10 minutes. You can use them to scratch the service of areas you want to learn and as a quick review when returning to something after a long absence.

Console Apps

For console applications, set $APPTYPE to CONSOLE.
--Mike Prestwood

In this tutorial, you will create a classic "Hello, World!" windows console application. A console application is a type of Windows application that has FULL access to the Win32 API, but it's GUI is limited to a DOS-like text window. When Windows starts a console application, it creates a text-mode console window where the program can display text and the user can interact with the program via the keyboard.

Setup

A console application can be as small as about 50KB.
--Mike Prestwood

To get started, you have to have Delphi installed. This article is based on Borland Developer Suite 2006 but you can use any version of Delphi you wish (there may be slight variations in menu options but we try to keep our 10 Minute Quick Starts as generic as possible).

If you don't have Delphi yet, you can download the trial edition for free:

And here, we, go...

Part 1: Simple Console Application

  1. Launch Delphi and click File | New | Other...
    Delphi File New Other
     
  2. In the dialog box, click Console Application then OK.
    Delphi New Items dialog, Console Application
     
  3. Notice the default code has a compiler directive that specifies the type of application (a console application). Alter the begin/end block as follows (see bolded code). Notice the ReadLn command which waits for the user to enter a value and press enter. In this case, we are using it to wait until the user presses ENTER.
     
    About $APPTYPE: For console applications, set $APPTYPE to CONSOLE. When set to console, the compiler generates a console application complete with the Input and Output standard text files automatically associated with it. The other valid value for $APPTYPE is GUI in which the compiler generates a graphical user interface application.

    program Hello;

    {$APPTYPE CONSOLE}
     
    uses
      SysUtils;



    begin
      WriteLn('Hello, World!');
      WriteLn('Press ENTER to exit.');
      ReadLn;



    end.

     
  4. Run-it! Now let's test your very first native code Windows program. You can run it within Delphi (called debugging) or you can run the EXE outside of Delphi. To run it, either click Run | Run, the green trianlge toolbar run icon, or press F9.
     
    Delphi Command Window

That's it! You've created your first console application using Delphi.
 

Part 2: Uses Clause and the Size of EXE

  1. Replace the three WriteLn and ReadLn code lines with the following code:

    ShowMessage('Hello, World!');
  2. Now click Run | Run to attempt to run the program. It will fail! So, don't panic. I wanted to show you what happens when you use a routine that's not currently available because you haven't yet included the needed unit your application. If you lookup the ShowMessage routine in the help system, you will that it is included in the Dialogs unit.
     
    About the Uses Clause: The uses clause lists the units currently available. You can include a uses clause in a program, library, or in a unit (either in the interface or implementation section). These units may in turn have uses clauses of their own. The System unit and the SysInit unit are used automatically by every application and cannot be listed explicitly in the uses clause.  
     
    Tip:  It's a good idea to open and become familiar with the code in commonly used units and is especially important with the System unit which implements string handling, floating point operations, file I/O, dynamic memory allocation, etc. Refer to the System unit help topic in Delphi's help system for more information.
     
  3. Alter your uses statement as follows:

    program Hello;
     
    {$APPTYPE CONSOLE}
     
    uses
      SysUtils, Dialogs;
     
    begin
      ShowMessage('Hello, World!');
    end.
     
  4. Now click Run | Run to run the program. Notice the console application is created, a Windows dialog is displayed, and once you click OK, the dialog closes and finally the console windows closes.
     
  5. Application size. A console application can be as small as about 50KB. Including Dialogs and using the ShowMessage routine increased the size of the application to include some Windows GUI overhead code. On my computer, it increased from 44KB to 404KB. Dialogs includes a lot more code than the 400KB in it and in the Pascal units it includes but the compiler optimizes out code you don't use. Because Windows GUI applications have a bit of overhead, you're simple console applications grew by nearly 10x.

Part 3: Using ReadLn with WriteLn

In this final console appliccation variation, we'll use ReadLn along with WriteLn to gather some information from the user. For good measure, we'll introduce Delphi's if statement and single and multi-line comments. Since Delphi is a strong typed language, we'll also use IntToStr to convert an integer to a string.

  1. Using what you learned so far, create a new console application using the following code:
     
    program Hello;
     
    {$APPTYPE CONSOLE}
     
    uses
      SysUtils, Dialogs;
    var
      FirstName: String;
      Age: Integer;
    begin
      //Gather name.
      WriteLn('What is your name?');
      ReadLn(FirstName);
     
      //Gather age.
      WriteLn('What is your age?');
      ReadLn(Age);
     
      WriteLn('Hi ' + FirstName + '. You are ' + IntToStr(Age));
     
      {
      Two apostropies= one in a literal string.
      Notice no statement ending semi-colon when preceding an else
        so as to continue the statement.
      }

      if Age < 14 then
        WriteLn('You need a parent''s permission to join our group.')
      else
        WriteLn('You are eligible to join our group.');
      WriteLn;
      WriteLn('Enter to exit.');
      ReadLn;
    end.

     
  2. Click Run | Run to run the program.

What Next?

If you wish to type in a simple console windows game, go to the following link:

That's-It! I hope you enjoyed this 10 minute quick start.

Linked Message Board Threads

 Delphi console app question in Delphi Object PASCAL MB Topic (2 replies)

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1257A1
Enter key:
Article Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101326 Counter
224577
Since 10/8/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]