Running Delphi Applications With Parameters

Though it was much more common in the days of DOS, modern operating systems also let you run command line parameters against an application so that you can specify what the application should do.

The same is true for your Delphi application, whether it be for a console application or one with a GUI. You can pass a parameter from Command Prompt in Windows or from the development environment in Delphi, under the Run > Parameters menu option.

For this tutorial, we'll be using the parameters dialog box to pass command line arguments to an application so that it'll be as if we're running it from Windows Explorer.

ParamCount and ParamStr()

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.

The OnActivate event handler of the main form is usually where the parameters are available. When the application is running, it's there that they can be retrieved.

Note that in a program, the CmdLine variable contains a string with command line arguments specified when the application was started. You can use CmdLine to access the entire parameter string passed to an application.

Sample 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. You can see that ParamStr "works" even if you haven't passed any parameters to the application; this is because the array value 0 stores the file name of the executable application, including path information.

Choose Parameters from the Run menu, and then add Delphi Programming to the drop-down list.

Note: Remember that when you pass parameters to your application, separate them with spaces or tabs. Use double quotes to wrap multiple words as one parameter, like when using long file names that contain spaces.

The next step is to loop through the parameters using ParamCount() to get the value of the parameters using ParamStr(i).

Change the button's OnClick event handler to this:

 procedure TForm1.Button1Click(Sender: TObject) ;

var

 j:integer;

 beginfor j := 1 to ParamCount do

 ShowMessage(ParamStr(j)) ;

 end;

When you run the program and click the button, a message appears that reads "Delphi" (first parameter) and "Programming" (second parameter).

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Running Delphi Applications With Parameters." ThoughtCo, Jan. 29, 2020, thoughtco.com/running-delphi-applications-with-parameters-1057665. Gajic, Zarko. (2020, January 29). Running Delphi Applications With Parameters. Retrieved from https://www.thoughtco.com/running-delphi-applications-with-parameters-1057665 Gajic, Zarko. "Running Delphi Applications With Parameters." ThoughtCo. https://www.thoughtco.com/running-delphi-applications-with-parameters-1057665 (accessed March 29, 2024).