Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
Does anyone know if (and how) it is possible
to retrieve the error code of a program with
a dos command ?(ie the code you return with the keyword
"return" in the main function of a program
written in C)

Well, first, the return code isn't really the same as the value you return with the return command. It is, for some compilers, yes, but mostly, it's ignored. If you want to be sure to return a code, use some assembly code or something, like this:
MOV AL,(return code)
MOV AH,4Ch
INT 21h
How to retrieve the error code, anyway, is firstly different depending on what technique or command you use to execute the program (there are a lot of interrupts and C functions for this and they all have different advantages and drawbacks)
Supposing you start the program from a command prompt, or from a bat file, the return code is saved in a sort or pseudo-variable called ERRORLEVEL.
This does not show up the way that you could write something like ECHO %ERRORLEVEL to show the result, but can (as far as I know) only be used with IF statements, and only as the comparision 'equal or greater', and as there are no ELSE either, code will be very messy but work, like this:IF ERRORLEVEL 4 ECHO Return code was greater than 3.
IF ERRORLEVEL 4 GOTO finish
IF ERRORLEVEL 3 ECHO Return code was 3.
IF ERRORLEVEL 3 GOTO finish
IF ERRORLEVEL 2 ECHO Return code was 2.
IF ERRORLEVEL 2 GOTO finish
IF ERRORLEVEL 1 ECHO Return code was 1.
:finishVery unpractical, but it's not my fault. Rather Microsoft's idea of constructing a flexible batch file language.

good point. i want a IF ERRORLEVEL = 1! :) btw, if anyone knows any *really good* C programmers, I can get the source to BATLITE 1.73 (batch compiler)

![]() |
Who is this Hamlet guy?
|
random computername in bo...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |