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. $SAMPLECODE$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16

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

Comments

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

This is so very helpful. THANK YOU!

This was just what i needed !

Thanks.

Thanks! Really useful piece of information. I've spent some time until found your advice and now my app works fine.

Excellent!!!

Really useful
Thanks!

Very very fine ... thank you very much

I'm testing a command line exe I compiled with VFP9 SP2, and it seems when I approach an overall arguments length greater than about 244 characters, the exe throws an error. Seems to be confirmed by last comment here where he talks about a 247 character limit:

http://social.msdn.microsoft.com/Forums/en-US/feeea559-4183-4fed-9081-3879e0d087de/command-line-parameters-length-limitation?forum=visualfoxprogeneral

Does anyone know if there's a fix for this?

It looks like the executable, which I created in VFP 9 SP2 does not pick up third parameter, which has space in it. I did enclose it in double quotation mark. The OS is Windows Server 2008 R2. Here is my command line: myexe administrator 7654321 "INFINITE IMAGING". The same exe works fine with these parameters on XP and Windows 7. Any help would be greatly appreciated.

Sorry, I realize this thread is ancient.

How do you pass an empty string or a space as one of the parameters? Lets say your program takes 4 parameters and they're organized positionally, such that param1 means a certain thing, etc. In your program, it is acceptable for one or more of the parameters to be blank or a space or an empty string, and be followed by non-empty parameters. Eg. "X:\Myapp\my.exe" 1234 " " 12/25/2002 .T.

The problem is that VFP seems to consider the empty parameter not there and moves the remaining parameters to the left, so to speak, so what your program's main parameter statement actually sees is "X:\Myapp\my.exe" 1234 12/25/2002 .T.

Can someone tell me how to deactivate the parameters in a foxpro exe? If I drop a file in my exe, a error is displayed. I don't want parameters, i want ignore it without errors. How I can do it?

The fix:
Locate the main file in your project - it will be in bold font, and can be either a PRG or a Form (SCX).
Add following code to the main PRG (if a Form, add to the Init method):
LPARAMETERS p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16
Do not add any other code: your program will now accept up to 16 parameters but not do anything with them.

Why it works:
Unlike other languages which have an Arguments() type call to retrieve parameters, VFP expects you to add the LPARAMETERS statement to accept any parameters your code may be passed. Hence you are adding code to accept any passed parameters, but not adding any code to do anything with them.