Share this page 

Return an ERRORLEVEL to a BAT file from a PB appTag(s): WinAPI/Registry

First method:
[External FUNCTION declaration]
SUBROUTINE ExitProcess(ulong uExitCode) LIBRARY "kernel32.dll"

ExitProcess(50)   // terminate the current application
                  // with ERRORLEVEL 50
[BAT file example]
echo off
start /w myapp.exe
if errorlevel 100 goto 100
if errorlevel 50  goto 50
echo exit code is 0
goto done
echo exit code is 100
goto done
echo exit code is 50
echo done.
NOTE: Errorlevel values must range between 0 and 255. Testing this API from inside the Powerbuilder environment (in development mode) will terminate PB itself.

One nice way to implement this is to have a global variable gul_rc with a default value of 0. This variable will be modified to contain a different value if needed. When executing the close event, a call is made to the ExitProcess subroutine passing the current value of gul_rc. Before it may be safer to call the Garbage collector to avoid some memory leaks because ExitProcess() is a rude way to terminate an application. You trigger the Garbage collector by calling the function GarbageCollect().

Second method:

[close event of the application]
Message.LongParm = 2   // return 2 as ERRORLEVEL