0

I have a c program compiled as say demo.exe which will be run using a batch file or a python script. The C program will return an error code which I need to catch in the batch file or a python script.

Below is a sample program for C on windows

#include <stdio.h>
#include <stdlib.h>

int func()
{
    return(44);
}

void fun(void)
{
        func();
}


int main(int argc, char *argv[])
{
    int c = atexit(fun);
        printf("c = %dn", c);

    return(74);
}

Next I wrote a windows batch file to run the program: I tried 3 scripts to run the exe but I am not getting return value from the C program. Instead the exe name is getting returned. This is the batch file:

@ECHO OFF

ECHO ==================================== FIRST BATCH TEST SCRIPT ====================================

call demo.exe
echo %ERRORLEVEL%
echo demo.exe returns "%ERRORLEVEL%"
IF %ERRORLEVEL% NEQ 0 (
  echo FAILED
)

echo ==================================== SECOND BATCH TEST SCRIPT ====================================

for /f %%a in ('demo.exe') do set "demo=%%a"
echo %ERRORLEVEL%

echo ==================================== THIRD BATCH TEST SCRIPT ====================================

cd %~dp0
call demo.exe

if "%ERRORLEVEL%" == "54" goto NO_ERROR

:NO_ERROR
echo No Error. Error code = %ERRORLEVEL%
PAUSE
goto end

:ERROR
echo ERROR. Error code = %ERRORLEVEL%
PAUSE
:end

Output is as follows:

C:\Users\USER4\Downloads>demo.bat
==================================== FIRST BATCH TEST SCRIPT ====================================
c = 0ndemo.exe
demo.exe returns "demo.exe"
FAILED
==================================== SECOND BATCH TEST SCRIPT ====================================
demo.exe
==================================== THIRD BATCH TEST SCRIPT ====================================
c = 0nNo Error. Error code = demo.exe
Press any key to continue . . .

ERRORLEVEL is always coming as the name of the exe. How to get the return value of the C program in the batch file?

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
RKum
  • 758
  • 2
  • 12
  • 33
  • why you have tagged it as `python`? – Shubham Shaswat Feb 18 '20 at 13:06
  • 2
    `ERRORLEVEL` is not `%ERRORLEVEL%`. See https://devblogs.microsoft.com/oldnewthing/20080926-00/?p=20743 – Paul Ogilvie Feb 18 '20 at 13:09
  • 1
    From the main you should `exit` not `return` if you want the exit code, like `exit(74)` – ChatterOne Feb 18 '20 at 13:13
  • 1
    Does this answer your question? [batch file test error level](https://stackoverflow.com/questions/6812484/batch-file-test-error-level) – Ruud Helderman Feb 18 '20 at 13:19
  • Please note that `Call "file.bat" [args]`, `Call "file.cmd" [args]`, `Call :BatchFileLabel [args]`, and `Call InternalCommand [args]` are how to use the `Call` command, not `Call "file.exe" [args]`. – Compo Feb 18 '20 at 13:32
  • 1
    @ChatterOne `return 74` [sets the exit code](https://stackoverflow.com/questions/4818888/main-functions-return-value). There _are_ [differences between exit and return](https://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main), but this is not one of them. – Ruud Helderman Feb 18 '20 at 13:48
  • Thanks everybody for their suggestions. In main instead of "return(74);" if I use exit() i.e. say "exit(func());" then it works for me and I get the value returned by main in %ERRORLEVEL%. – RKum Feb 18 '20 at 14:56

0 Answers0