Microsoft KB Archive/42472

= Basic Cannot Get Child Process Return Code or &quot;Error Level&quot; =

Article ID: 42472

Article Last Modified on 11/21/2006

-

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 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 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 &quot;error levels&quot; 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 &quot;commandstring&quot; 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 (&quot;Get Return Code&quot;). 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 &quot;errcode.exe&quot;    ' 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 &quot;The error level returned was &quot;; 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[] = {&quot;inside of the c routine\n&quot;}; printf(&quot;%s&quot;, 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

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

Keywords: KB42472

-

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

© Microsoft Corporation. All rights reserved.