Re: Get dos errorlevel

 BBS: Inland Empire Archive
Date: 02-23-94 (23:25)             Number: 298
From: GARY GLUECKERT               Refer#: NONE
  To: HARRY F. HARRISON             Recvd: NO  
Subj: Re: Get dos errorlevel         Conf: (2) Quik_Bas
HF> > Help! I've been trying to find a way to get the dos errorlevel and
HF>am  > having some problems. I'm using INT 21H, AH=4DH as shown below.
HF>This  > interrupt is known as the GET Child-Program Return Value and
HF>The problem is this:
HF>COMMAND.COM always returns 0.

Not so: see the code below which I posted earlier:
; MonitorShell (MONSHELL.ASM)

; Written by Tony Elliott

; EllTech Development, Inc.
; 4374 Shallowford Industrial Parkway
; Marietta, GA 30066
; (404) 928-8960 Voice or (404) 928-7111 BBS (HST)

; Released into the public domain.

; This routine is written for Microsoft QuickBASIC 4.x, BASIC 6.x and
Microsoft ; PDS 7.x. Assemble using MASM 5.1:  MASM MONSHELL;

; You can link it directly to your program, place it in a LINK library
(.LIB) ; or a .QLB.

; To use it:

;   DECLARE SUB MonitorShell ()
;   DECLARE FUNCTION FetchErrorLevel% ()
;   CALL MonitorShell
;   SHELL "PkZip -Xxy"
;   PRINT "ErrorLevel returned from SHELL:";FetchErrorLevel

.model medium, basic

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

    ; From BASIC: CALL MonitorShell

    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


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

    jmp dword ptr [Old21]       ;Transfer control to original Int 21h

MonitorShell endp

FetchErrorLevel proc

    ;From BASIC: DECLARE FUNCTION FetchErrorLevel%
    ;            PRINT "Last errorlevel:"; FetchErrorLevel%

    mov al,cs:ExitCode          ;Put the errorlevel into al
    xor ah,ah                   ;Zero ah
    ret                         ;ErrorLevel returned as result

FetchErrorLevel endp

The errorlevel is set in DOS by the last process. There are actually
two variables set - The termination method of the last process and it's
DOS errorlevel. Note that you will receive 0 as the DOS errorlevel in
EVERY case if you do not set the interrupt vector first.
Try  the little ditty above, if you don't have the compiled OBJ file
then you can freq it from here with the magic name of MONSHELL or you
can d/l from my basic programming directory.
FIDONet    1:107/270, FREQ=MONSHELL
BBS Phone  516-420-0818, Download filename=FETCHERR.ZIP
Gary G.

--- MsgToss 2.0c
 * Origin: The State University of New York at Farmingdale (1:107/270)
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