Jump to content

[delphi] Simple Console App


Recommended Posts

Requires Delphi 4 (Delphi 5 and 6 PE are 100% free to use for non-profit purposes) or Kylix (Except Linux does not support ShellAPI)

Delphi, like PHP, has to have ever line ending with a semi-colon.

This particular program is used to launch non-console apps like winver

Project1.dpr:

program Project1;
{$APPTYPE CONSOLE}

uses
 SysUtils, ShellAPI;

const
CrLf = #10 + #13;

var
varI : String;

function MainFunction(): Integer;
begin
  System.Write('C:\>');
  system.Readln(varI);
  ShellExecute(0, '', pchar(VarI), '', nil, 1);

end;


begin

       repeat

        MainFunction();
       until varI = 'exit';


end.

Delphi might be considered one of the more stranger languages, but it sure makes up for that with the simplicity of a simple GUI builder and complex data type.

Lets start from the beggining:

program Project1;

This tells Delphi the name of the program, it is crucial that this is also the name of the project file (Project1.dpr)

{$APPTYPE CONSOLE}

Pretty self explanitory, this tells the Delphi compiler that this is a console application

uses
 SysUtils, ShellAPI;

This tells us what units to use (sysutils.dcu, shellapi.dcu), these are like compiled libraries. Delphi has a VB like function and procedure completing system so you can type:

shellapi.

And you will get a drop down list of all the goodies availiable.

const
CrLf = #10 + #13;

This part is useless for now, it is simple to show how to declare a program constant. THis particular constant is what I like to use to make a new line (Carriage Return, Line Feed)

var
varI : String;

To declare app wide variables, use the above. In this case, VarI is a String.

NOTE: Delphi has hundreds of variable type, like integer,pchar, boolean, extended, etc...

function MainFunction(): Integer;
begin
  System.Write('C:\>'); //Writes a line without a CrLf at the end
  system.Readln(varI); //puts the input from user into varI
  shellapi.shellExecute(0, '', pchar(VarI), '', nil, 1); //standard win32 shellapi, except VarI is cast as a pchar (sorta like a string)

end;

This a a Delphi sample function, with a result type of Integer (I just used this for an example).

This shows the use of the System and shellapi library; you can just use Write and Shellexecute instead of using the library prefix. The Readln function puts the intput of the line into the variable.

begin

       repeat

        MainFunction();
       until varI = 'exit';


end.

This is the first part of the program which is run, it just repeats the call to MainFunction() untill someone types exit, whenever somthing is typed it is put into the variable varI as an integer. All Delphi procedures and functions fallow this format

Function somthing: string;

var

foo: boolean;

begin

.......

end;

And there you have your basic example of Delphi code; if you have used VB it should not take more than a few weeks to learn stuff like this.

Any question or comments?

Dont hesitate to ask :)

Link to comment
Share on other sites

  • 1 year later...

means a] require delphi 4 b] Personal Edition is only for personal use and you cannot market your software for money if it was made in that version..

anyways, i didnt notice this code.. but nice zivan, even if it was posted in 2003 :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...