1. Technology

Running Delphi Applications With Parameters

How to pass command-line parameters to your application and how to handle them.

By

In the days of DOS it was a common practice run applications (command line programs) with some kind of parameters that will specify want we want to do. Even now, in the world of Windows, we can go to MS-Dos prompt and run MS-DOS based program like DIR /?. That '/?' after program name (DIR) will give us some help regarding the usage of the DIR command.

In this article, we will find out how to respond to command line parameters passed to a Delphi application.

Parameters

We can pass the parameter from the command line in Windows or from the development environment in Delphi under Run-Parameters menu option.

We will use Parameters dialog box to pass command-line parameters to an application when we run it (for testing purposes - from within Delphi), just as if we were running the application from the Windows Explorer.

ParamCount, ParamStr()

Simply put, the ParamCount function returns the number of parameters passed to the program on the command line, and ParamStr returns a specified parameter from the command-line.

While application is running, the parameters are available to us so we can retrieve them within a specific section of the application (usually from the OnActivate event handler of the main form).

Note: In a program, the CmdLine variable contains a string with command-line arguments specified when the application was started. We can use CmdLine to access the entire paramstring passed to an application.

We'll start with a simple application. Start up a new project and place a Button component on Form. In the button's OnClick event handler, write the following code:

 procedure TForm1.Button1Click(Sender: TObject) ;
 begin
    ShowMessage(ParamStr(0)) ;
 end;
 
When you run the program and click the button, a message box appears with the path and file name of the executing program.

We can see, that even if we haven't passed any parameters to our application ParamStr function "works", the reason is that the array value 0 stores the file name of the executable application including path information.

Now, choose Parameters from the Run Menu and add 'Delphi Programming' to the drop down list (without apostrophes).

Note: when you pass parameters to your application separate them with spaces or tabs. Use double quotes to wrap multiple words as one parameter (such as long file names containing spaces).

We will be looping through the amount of parameters using ParamCount() to get the value of parameters passed, with ParamStr(i).

Change the button's OnClick event handler to:

 procedure TForm1.Button1Click(Sender: TObject) ;
 var
    j:integer;
 begin
    for j := 1 to ParamCount do
      ShowMessage(ParamStr(j)) ;
 end;
 
When you run the program and click the button, a message box appears displaying 'Delphi' (first parameter) and 'Programming' (second parameter).

Note: Working with parameters passed to the console mode application is the same.

That's it, simple as only Delphi can be!

©2014 About.com. All rights reserved.