How to
create a console mode program
See Also
• Find out where your program is running from
• How to look for and handle command line
parameters
You can
write console mode programs, also called command line programs, programs that
usually does not use Windows' graphical user interface, using Delphi.
Create a
new application using "File | New Application"
Go to the
"Project Manager" ("View | Project Manager")
Remove the
default form from the project (highlight the unit and hit DELETE -- do not save
changes)
Go to
"Project Source" ("View | Project Source")
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}
uses
SysUtils;
begin
{ add your code here... }
WriteLn( 'hello, world!' );
end.
Listing #1
: Delphi code. Right click console.pas to download.
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:
{$IFDEF
CONSOLE}
{... add your console mode code here ...}
{$ENDIF}
Listing #2
: Delphi code. Right click ifdef.pas to download.
If you want
to check for the same condition during run time, use the "IsConsole"
variable. Example:
if(
IsConsole )then
begin
{... add your console mode runtime code here
...}
end;