END [exitcode] -- reading the program exit or error code

Share your advanced PureBasic knowledge/code with the community.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

END [exitcode] -- reading the program exit or error code

Post by Keya »

cos i couldnt find an existing example and was wondering

EXITWITHCODE.PB - PB app that exits with an error code

Code: Select all

#ERRCODE = 123
OpenConsole()
PrintN("Exiting with error code " + Str(#ERRCODE))
End #ERRCODE
The following three demos simply execute the above app and read/display its error code...

GETEXITCODE.PB - PB app that runs a program and reads its error code

Code: Select all

OpenConsole()
hRunApp = RunProgram("exitcode.exe", "", "", #PB_Program_Wait | #PB_Program_Open)
PrintN("The program exited with code " + Str(ProgramExitCode(hRunApp))): Input()
GETEXITCODE.BAT - Windows batch file that runs a program and reads its error code

Code: Select all

exitcode.exe
@echo The program exited with code %errorlevel%
GETEXITCODE.SH - Linux & Mac bash script that runs a program and reads its error code

Code: Select all

#!/bin/bash
./exitcode
echo "The program exited with code $?"
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: END [exitcode] -- reading the program exit or error code

Post by netmaestro »

The 'exit code' can actually be used for one more (afaik undocumented) use, which I hope will prove self-explanatory:

Code: Select all

Procedure Cleanup()
  Debug "Cleaning up this"
  Debug "cleaning up that"
EndProcedure

Debug "Main Loop..."

End Cleanup()
BERESHEIT
Post Reply