ParamStr - Run Delphi Application with Command line paramaters

ParamStr(ParamIndex : integer)

The ParamStr function returns one of the parameters from the command line used to invoke the current program.

The ParamIndex parameter determines which parameter is returned:

0 : The execution drive/path/program name. Returns same as Application.ExeName.

1 : Return the 1st parameter 2 : Return the 2nd parameter ...

If there is no parameter value for the given index,an empty string is returned.

// Before running this code, use the Run/parameters menu option
// to set the following command line parameters : -parm1 -parm2

ParamCount()
The ParamCount function gives the number of command line parameters used to invoke the current executable.

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).

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.

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).

Command Line Parameters within the IDE
Open project in Delphi IDE
Just go to "Run > Parameters" via the menu of the IDE.
In the field appearing in the dialog window, you can just write the parameters as you would also type them into the command line.



Command Line Parameters from Command Prompt
Start -> Run -> Cmd -> Go to Project Path and type



Important to know: 
"ParamStr(0)" always contains the full path of the application, as it can theoretically  also be seen in the command line. After that, our two parameters come in "ParamStr(1)" and "ParamStr(2)".
If we call ParamStr() with a value greater than ParamCount, an empty string is returned.



Comments

Popular posts from this blog

ShellExecute in Delphi

MS Excel Automation in Delphi

Drawing Shapes in Delphi