Back

How To Create Your First Console Application!


A Delphi Console program is different from DOS because it is able to call Windows API functions and also use its resources.

Okay the first thing we need to do is start a new project.
Save it as Project1 or whatever you want to name it.

You will see the new Form and Unit, we do not need the Form for a Console application so we need to get rid of it and the way to do this is to (with your mouse cursor) select (from the File menu) Project (this may be different for different versions of Delphi) and then click on Remove From Project.

You have only one Form in your project so select it and click on OK.
A 'Save Changes To Unit1.Pas ' dialog box may appear at this stage, click 'No'.
At this point you have removed Form1 from your project, you now have a 'Formless Application'.

Have a look at the source code for your project in the Delphi Code Editor.
You should see this:



program Project1;

uses
  Forms;

{$R *.RES}

begin
  Application.Initialize;
  Application.Run;
end.



Note we are still using the Forms unit, this is no longer needed since our application is Formless.
So go ahead and delete the uses clause.
Your program code should now look like this:


program Project1;

{$R *.RES}

begin
  Application.Initialize;
  Application.Run;
end.



The next change we need to make is to remove 'Application.Initialize;' and 'Application.Run;'.
Why?


program Project1;

uses
  Forms; //Delete this!

{$R *.RES}

begin
  Application.Initialize;      // Delete this!
  Application.Run;           // Delete this!
end.



Application.Initialize is used to initialize OLE servers and since we don't use OLE in this Console application this line can be removed.
Application.Run is used to show the Form and we are not using Forms so delete this as well.
We use the $AppType compiler directive to change our application to Console mode, we add it like this:


program Project1;

{$AppType Console}

{$R *.RES}

begin

end.



Although this looks very bare you have just created a basic Console application if you run it as it is you will not see much so we had better add some more code to make it do something.
Lets add the words "My first Console Application!" using WriteLn.
Turbo Pascal programmers from the past and present should know WriteLn.
Your code should look like this:


program Project1;

{$AppType Console}

{$R *.RES}

begin
  WriteLn('My first Console Application!');
end.



Run the program now.
Briefly you will see a Dos style window with the words 'My first Console Application!'.
To make the Dos style window visible until we do something (press a key) add ReadLn.
Your code should look like this:


program Project1;

{$AppType Console}

{$R *.RES}

begin
   WriteLn('My first Console Application!');
   ReadLn;
end.



Run the program again and when you have read the text press the ENTER key and the window will disappear.
Amazing wasn't it, simple yet useful.


Thats it.


Franz-Leo Chomse <[email protected]> had these comments to make about How To Create Your First Console Application!

Remark 1

In Delphi 5

File | New | Console Application

will create the program template for you, without the editing needed in the tip.

Remark 2

One Application can be both a Console Application and a GUI Application. In this case a additional Console Window
is automatically created and assigned to standard input and standard output. This can be very usefull for debugging via
simple WriteLn statements.

One caveat exists: Closing the console windows terminates the application.

Remark 3

Windows ME has still the DOS Window, but you can't boot into DOS mode any longer. For a pure DOS environment you neet a start diskette.

Remark 4

If you start a Console application form the DOS prompt or a batch file you have to use the start command to create a new
console window for the application. Otherwise it takes over the DOS prompt window from which it was started.

For task tray applications you have to use start in batch jobs but not in the DOS prompt window (NT 4).
So they seem to be somewhere between a pure GUI application and a Console application.

Remark 5

Console Applications are still fullblown Windows applications and no MSDOS applications.
 

Regards from Germany

Franz-Leo



Back
Hosted by www.Geocities.ws

1