Wednesday, March 2, 2011

How to create a console mode program

You can write console mode programs, also called command line programs, programs that usually does not use Windows' graphical user interface, using Delphi.

  1. Create a new application using "File | New Application"
  2. Go to the "Project Manager" ("View | Project Manager")
  3. Remove the default form from the project (highlight the unit and hit DELETE -- do not save changes)
  4. Go to "Project Source" ("View | Project Source")
  5. Edit your project source file:
    • Remove code inside "begin" and "end"
    • Replace the "Forms" unit in the "uses" section with "SysUtils"
    • You probably don't need to load the resource file -- remove "{$R *.RES}"
    • Finally place "{$apptype console}" in a line by itself right after the "program" statement.
You just created a console mode program skeleton in Delphi. Now you can add your code inside "begin" and "end." statements.
program console;

{$apptype console}


  { add your code here... }
  WriteLn( 'hello, world!' );
Listing #1 : Delphi code. Download console (0.24 KB).
By the way, you can add conditional code to your program that would get executed depending on the type of your application, if you happened to write a dual mode program that has a console mode version and a graphical (GUI) version.
For example, if you want to include a piece of code to your program only if it's a console mode program try this:

  {... add your console mode code here ...}

Listing #2 : Delphi code. Download ifdef (0.19 KB).
If you want to check for the same condition during run time, use the "IsConsole" variable. Example:
if( IsConsole )then

  {... add your console mode runtime code here ...}

Listing #3 : Delphi code. Download iscnsole (0.21 KB).

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum