=====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%20for%20an%20external%20program%20to%20terminate|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.