Basic Cannot Get Child Process Return Code or "Error Level" (42472)



The information in this article applies to:

  • Microsoft QuickBASIC 2.0
  • Microsoft QuickBASIC 2.01
  • Microsoft QuickBASIC 3.0
  • 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 (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1

This article was previously published under Q42472

SUMMARY

A parent process (such as a SHELLing program compiled in Basic) cannot determine what MS-DOS return code (MS-DOS error level) was set by the child (SHELLed) process. For example, if a parent Basic program SHELLs to a child process (such as a Basic, C, or MASM program), when the child terminates, the parent cannot determine the MS-DOS return code.

MORE INFORMATION

Languages such as C or Microsoft assembly language allow a program to set a return code as the program terminates back to the MS-DOS command line. The only Microsoft Basic product capable of setting an MS-DOS return code is Microsoft Basic PDS version 7.0 or 7.1. These return codes are referred to as "error levels" in MS-DOS batch file processing. For an example of returning an error level from a Basic PDS program to an MS-DOS batch file, query on the following words:

ERRORLEVEL and Basic

While Basic PDS can return an error level to an MS-DOS batch file, it (along with the other Microsoft Basic languages) cannot return an error level to a parent (SHELLing or spawning) process.

For example, Basic can execute a child process with the SHELL "commandstring" statement. When the child process terminates, the only means to determine the returned error code would be a CALL INTERRUPT to the MS-DOS interrupt 21 hex with function 4D hex ("Get Return Code"). Calling this function from Basic will execute with no adverse effects, but will not retrieve the correct return code.

The following program demonstrates that Basic cannot retrieve its child process return code or error level.
'*********************** CODE.BAS **************************
'
' You must use QB.LIB or QB.QLB for this program.
'
' Under the QBX.EXE environment of Basic PDS 7.0/7.1,
' the libraries above are QBX.LIB and QBX.QLB, respectively.
' Additionally, the $INCLUDE filename below would be 'QBX.BI'

REM $INCLUDE: 'qb.bi'
DIM inregs AS RegType, outregs AS RegType
CLS
SHELL "errcode.exe"     ' See C routine below.
inregs.ax = &H4D00
CALL INTERRUPT(&H21, inregs, outregs)
' Blank out AH. The code is returned in AL
outregs.ax = outregs.ax AND &HFF
PRINT "The error level returned was "; outregs.ax
END


/***
   Name this C program ERRORCODE.C and ERRORCODE.EXE.
   When this routine is executed as part of a batch file,
   its return code is correctly interpreted when this
   program is SHELLed to by the above Basic program.
***/ 

int
main(void)
{
static char prompt[] = {"inside of the c routine\n"};
printf("%s", prompt);
return(5);
}

REM ************************* TEST.BAT *************************
ECHO OFF
REM This batch file for MS-DOS demonstrates that the C routine above
REM correctly returns an error level of 5.
ERRCODE.EXE
IF ERRORLEVEL 5 ECHO THE RETURN CODE IS 5
				

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB42472