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.
 
* 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.
 
PCOUNT( ) Function:Returns the number of parameters passed to the current program,
 procedure, or user-defined function.
PCOUNT( )
 

Example 1

DO testpar WITH 1,2,3

PROCEDURE testpar
PARAMETERS gn1,gn2,gn3
gcMessage = 'PCOUNT( ) ='+ALLTRIM(STR(PCOUNT( )))
WAIT WINDOW (gcMessage)
RETURN

Example 2

SET TALK OFF
gnVal1 = 10
gnVal2 = 20
gnVal3 = 30
gnVal4 = 15
gnMin = getavg(gnVal1, gnVal2, gnVal3, gnVal4)
? 'Average value is '
?? gnMin

* This user-defined function permits up to 9 parameters to be passed.
* It uses the PCOUNT( ) function to determine how many
* were passed and returns the average value.

FUNCTION getavg
PARAMETERS gnPara1,gnPara2,gnPara3,gnPara4,gnPara5, ;
   gnPara6,gnPara7,gnPara8,gnPara9
IF PCOUNT( ) = 0
   RETURN 0
ENDIF
gnResult = 0
FOR gnCount = 1 to PARAMETERS( )
   gcCompare = 'gnPara' +(STR(gnCount,1))
   gnResult = gnResult + EVAL(gcCompare)
ENDFOR
gnResult = gnResult / (gnCount - 1)
RETURN gnResult
 

Không có nhận xét nào:

Đăng nhận xét