Invoking Programs with Arguments from Basic Without CHAIN/RUN (77834)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 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 "NEXTAPP.EXE Arg1 Arg2 ..."
    						
  • 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:

    1. 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.
    2. 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 = "anything.exe"
         if (condition1) then
                 Arg1 = "Argument1"
                 Arg2 = "Argument2"
         elseif (condition2) then
                 Arg1 = "Argument3"
                 Arg2 = ""
         elseif ...
         ...
         OPEN "BATCH.BAT" FOR APPEND AS #1
         PRINT #1, NameOfNextApp$ + " " + Arg1 + " " + 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 "anything" 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
      								
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.

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB77834