belltric belltric - 3 months ago 26
Vb.net Question

Get Return Value from Console Application in Batch file

I'm trying to get return value from console application in batch file. Console application keep returns 0 value even when the rtnValue is 1 in console application

Console Application Code



Public Function Main() As Integer
Dim rtnValue as Int16 = 0

if Not Process Then rtnValue = 1

Return rtnValue

End Function


Batch File



@ECHO OFF

start /d "%~dp0" ConsoleApp.exe

if "%ERRORLEVEL%" == "0" goto success

:success
echo Success with error code: %ERRORLEVEL%
PAUSE
goto end

:error
echo Failed with error code: %ERRORLEVEL%
PAUSE

:end

Answer

Don't use start /d "%~dp0" ConsoleApp.exe that is launching a new shell to run it in. Instead just use

cd %~dp0
ConsoleApp.exe

and then test for the exit code.