Microsoft KB Archive/77834

= Invoking Programs with Arguments from Basic Without CHAIN/RUN =

Article ID: 77834

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q77834



SUMMARY
Because the CHAIN and RUN statements do not allow passing command-line arguments to the target program, you can instead use the SHELL statement (which allows arguments), a CALL INTERRUPT or POKE to stuff the keyboard buffer, or batch files.



MORE INFORMATION
Below are three ways to launch an application with command-line arguments.

  Use the SHELL statement. The main disadvantage of the SHELL statement is that the Basic program that uses SHELL remains in memory while the SHELLed-to program is executed. The Basic program resumes after you exit the SHELLed-to program. SHELL &quot;NEXTAPP.EXE Arg1 Arg2 ...&quot;  Use CALL INTERRUPT or POKE to stuff the keyboard buffer with a string of characters containing the name of the executable and arguments or batch file to launch. This method limits the resulting command line, because only 16 characters can be put into the keyboard buffer at once. For details on CALL INTERRUPT and POKE, query in this knowledge base on the following words:

Basic and KEYBOARD and BUFFER and INTERRUPT



Also, you can use CALL INTERRUPT or POKE to put command-line arguments into the Program Segment Prefix (PSP). For more information on this method, query on the following words:

Basic and CHAIN and RUN and PSP

 Use DOS batch files to control the launch of other applications. While batch file methods tend to be less technically involved than a CALL INTERRUPT, they require an understanding of batch-file programming, and require additional files, namely the batch files, which may be a disadvantage for some people. Below are two ways to use the batch files:

  Start the Basic program from within a batch file. Then, use the END n% statement in the Basic PDS 7.0 or 7.1 program to stop execution. This will set the DOS ERRORLEVEL parameter to n%, which can be checked inside the batch file. (The END n% statement to set the DOS ERRORLEVEL is NOT supported in QuickBasic 4.5 or earlier; it is just supported in Basic PDS 7.0 or 7.1.)

BATCH1.BAT
BasicPRG.EXE         REM this calls the Basic executable REM You must test the highest error levels first, since REM a test for ERRORLEVEL=1 will be true for all error REM levels 1 or higher. if ERRORLEVEL = 2 then NEXTAPP.EXE Arg3 Arg4 ... goto exit if ERRORLEVEL = 1 then NEXTAPP.EXE Arg1 Arg2 ... goto exit if ...  ... exit: REM ***** end of batch *****

BasicPRG.BAS
if (condition1) then n% = 1 elseif (condition2) then n% = 2 elseif ...  ... END n%         '  Any number of ENDs can be used ' anywhere in the Basic program REM ***** end of BasicPRG ***** The disadvantage of this method is that the arguments in the batch file above are fixed. While this can be used in some situations, in others, users need the arguments to be dynamically changed inside the Basic program. If this is the case, method b below is an option.   A batch file can be used with the Basic program to facilitate the use of passing dynamically determined arguments to other applications.

BATCH.BAT
REM ***** BATCH.BAT ***** BasicPRG.EXE     REM start the Basic program REM ***** end of batch1 *****

BasicPRG.BAS
NameOfNextApp = &quot;anything.exe&quot; if (condition1) then Arg1 = &quot;Argument1&quot; Arg2 = &quot;Argument2&quot; elseif (condition2) then Arg1 = &quot;Argument3&quot; Arg2 = &quot;&quot; elseif ...  ... OPEN &quot;BATCH.BAT&quot; FOR APPEND AS #1 PRINT #1, NameOfNextApp$ + &quot; &quot; + Arg1 + &quot; &quot; + Arg2 CLOSE #1 END REM ***** end of BasicPRG ***** After the BasicPRG completes execution, BATCH.BAT contains the name of the application to launch, including the arguments to be used. For example, if condition1 was true in BasicPRG, BATCH.BAT would appear as follows: REM ***** BATCH.BAT ***** BasicPRG.EXE     REM start the basic program REM ***** end of batch1 ***** anything.exe Argument1 Argument2 This would launch an application called &quot;anything&quot; with the two arguments Argument1 and Argument2. Similarly, if condition2 is true in BasicPRG, BATCH2.BAT will appear as follows: REM ***** BATCH.BAT ***** BasicPRG.EXE     REM start the basic program REM ***** end of batch1 ***** anything.exe Argument3 </ol> </li></ul>

Because of the order of the programs in BATCH.BAT, ANYTHING.EXE will be started after BasicPRG.EXE completes execution. This is a tricky method, as it may seem that BATCH.BAT cannot be opened while it is being used.

Additional query words: QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Keywords: KB77834

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.