Rtrv ErrLvl from SHELL

 BBS: Inland Empire Archive
Date: 03-31-91 (03:31)             Number: 158
From: BRENT ASHLEY                 Refer#: NONE
  To: ALL                           Recvd: NO  
Subj: Rtrv ErrLvl from SHELL         Conf: (2) Quik_Bas
There has been much discussion here about getting the errorlevel
returned by a shelled program, but no answers yet.  I found this
tidbit some time ago on the Ilink network, from Tony Elliott of
Elltech.  It works well for me.  Next message contains DEBUG script.
;
; GetErr.ASM - routines to retrieve errorlevel of SHELLed program
;
; by Tony Elliott - captured from a message on ILink QB echo
;
; put the following declarations in your QB program:
;
; DECLARE SUB MonitorShell ()
; DECLARE FUNCTION FetchErrorLevel% ()
;
; use as follows:
;
; CALL MonitorShell
; SHELL YourCmdLine$
; ErrLvl% = FetchErrorLevel%
;
; The MonitorShell routine hooks INT 21h and checks for service 4Ch,
; the exit-with-errorlevel request.  It then saves the ExitCode
; passed, unhooks itself, and allows the interrupt to continue.  It
; will therefore only retrieve the first errorlevel set during the
; shell - remember this if shelling to a batch.
;
; The FetchErrorLevel routine simply reports the saved ExitCode.
;
.model medium, basic
.code
Old21        Label Dword            ;Label pointing to old Int 21h handler
Old21Offset  dw ?                   ;Offset part
Old21Segment dw ?                   ;Segment part
MonitorSet   db 0                   ;Our "hooked" flag
ExitCode     db ?                   ;Where we store the exit code

MonitorShell proc uses ds
        cmp cs:MonitorSet,0            ;Are we already hooked?
        jnz MonitorExit                ;If so, exit
        mov ax,3521h                   ;Get current vector for int 21h
        int 21h
        mov cs:Old21Segment,es         ;Remember it for later
        mov cs:Old21Offset,bx
        mov ax,2521h
        push cs
        pop ds                         ;Point int 21h handler to our code
        mov dx, offset OurInt21
        int 21h
        mov cs:MonitorSet,-1           ;Set our flag

MonitorExit:
        ret

OurInt21:                              ;Our Int 21h handler
        cmp ah,4ch                     ;Is it a 'terminate' request?
        jnz Continue                   ;If not, continue on

        mov cs:ExitCode,al             ;Remember the exit code
        push ax
        push ds
        mov ax,2521h            ;Unhook ourself after the first hit
        mov ds,Old21Segment
        mov dx,Old21Offset
        int 21h                 ;Point Int 21h back to original handler
        pop ds
        pop ax
        mov cs:MonitorSet,0     ;Set out flag back to zero

Continue:
        jmp dword ptr [Old21]   ;Transfer control to original Int 21h
MonitorShell endp

FetchErrorLevel proc
        mov al,cs:ExitCode      ;Put the errorlevel into al
        xor ah,ah               ;Zero ah
        ret                     ;ErrorLevel returned as result of function
FetchErrorLevel endp
        end

; [end of file]
___
 X DeLuxe2 1.12 #10383 X

--- Maximus 2.00
 * Origin: Durham Systems (ONLINE!) (1:229/110)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET