How to pass parameters to VFP EXE

Passing command-line parameters from Windows to VFP created EXE is different from passing parameters to VFP programs or functions in VFP application:

  • All parameters are passed to EXE as strings
  • Parameters should not be enclosed in single quotes because quotes will be treated as part of parameter
  • Parameters with spaces can be enclosed in double quotes
  • Parameters are separated by spaces not commas

In order to accept command-line parameters VFP application main program must begin with LPARAMETERS or PARAMETERS statement. A VFP form cannot be main in VFP application in this case because command-line parameters are not passed to the form's Init method.
It's not recommended to use a switch like parameters (that starts with '-' or '/') because they may conflict with VFP command-line switches. The complete list of VFP switches can be found in Command-Line Switches topic in VFP help. Martina Jindrù offers interesting alternative in Named Parameters.

Here's an example that shows how to pass parameters and accept them in VFP EXE.

This is sample code. Add error handling and adjust to your requirements as necessary.

* Shortcut's command-line box
"X:\Myapp\my.exe" 1234 "two items" 12/25/2002 .T.
 
* Main program of my.exe
LPARAMETERS tcPar1, tcPar2, tcPar3, tcPar4, tcPar5, tcPar6
 
* Parameter | Value       | Type | How to convert                  VFP 9.0
*-----------------------------------------------------------------------------------
* tcPar1      "1234"         C     INT(VAL(tcPar1))                CAST(tcPar1 AS I)
* tcPar2      "two items"    C      N/A                             N/A
* tcPar3      "12/25/2002"   C     EVALUATE("{" + tcPar3 + "}")    CAST(tcPar3 AS D)
* tcPar4      ".T."          C     EVALUATE(tcPar4)                CAST(tcPar4 AS L)
* tcPar5      .F.            L      N/A                             N/A
* tcPar6      .F.            L      N/A                             N/A

When less parameters passed to EXE than specified in the LPARAMETERS, the rest of parameters will have logical type and .F. value. The PCOUNT() function can be used to determine the number of passed parameters.

Your rating: None Average: 4.5 (2 votes)

Thanks

Short and sweet. Thanks Sergey. I've been looking for information everywhere on exe command line args for VFP apps. Very little information around.

Thank you...

This is so very helpful. THANK YOU!

Thank You!

This was just what i needed !

Thanks.