User Tools

Site Tools


reading_20the_20exit_20code_20of_20a_20dos_20utility

This is an old revision of the document!


Reading the exit code of a DOS utility

by Richard Russell, May 2006

BBC BASIC for Windows allows you to execute an external program using the *RUN command. The program can be a Windows GUI program, a Windows console program or an MS-DOS program. Both Windows console programs and MS-DOS programs have the ability to return an exit code which may, for example, indicate whether the program completed successfully or not. Listed below are three methods of discovering the exit code:

Method 1


The routine below allows the exit code to be discovered by running the external utility via a batch file:

      F% = OPENOUT "RUN.BAT"
      PRINT #F%, program$ : BPUT #F%,10
      PRINT #F%, "ECHO %ERRORLEVEL% > EXITCODE.TXT" : BPUT#F%,10
      CLOSE #F%
      *RUN RUN.BAT
      F% = OPENIN "EXITCODE.TXT"
      INPUT #F%, exitcode$
      CLOSE #F%
      exitcode% = VAL(exitcode$)

The variable program$ should contain the name of the external program plus any command line parameters it needs. Note that for this routine to execute correctly the current directory must be valid and writeable.

Some standard DOS commands return exit codes which you may wish to test. For example the XCOPY command returns one of the following values:

  • 0: Files were copied without error.
  • 1: No files were found to copy.
  • 2: The user pressed CTRL+C to terminate xcopy.
  • 4: There is not enough memory or disk space, or syntax error.
  • 5: Disk write error occurred.


Method 2


Run the external program using the function FNexecuteandwait which is documented in the article Waiting for an external program to terminate (scroll about two-thirds of the way down). The function returns the exit code.

Method 3

by Jon Ripley, May 2006

Add code similar to the following to the initialisation routine of your program:

      SYS "GetModuleHandle", "MSVCRT.DLL" TO hdll%
      SYS "GetProcAddress", hdll%, "system" TO hsys%

Here hdll% the handle of the C run-time library used by the BBC BASIC interpreter and hsys% is a pointer to the system() function the library provides.

To execute a command anywhere in your program use code similar to the following:

      SYS hsys%, program$ TO exitcode%

The variable program$ should contain the name of the external program plus any command line parameters it needs. When the program returns exitcode% will contain the exit code of the program.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
reading_20the_20exit_20code_20of_20a_20dos_20utility.1522502377.txt.gz · Last modified: 2024/01/05 00:16 (external edit)