0

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
belltric
  • 115
  • 2
  • 15
  • The `START` command does _not_ return the value of the executed command (it return 9059 if the command does not exist). You may use `START /WAIT` or entirely eliminate the START command. Further details in the **Table 4** of [this answer](http://stackoverflow.com/questions/34987885/what-are-the-errorlevel-values-set-by-internal-cmd-exe-commands/34987886#34987886) – Aacini Aug 15 '16 at 11:36

1 Answers1

1

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.

FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • When i replace start /d "%~dp0" ConsoleApp.exe with cd %~dp0 ConsoleApp.exe it gets "The Sytem cannot find the path specified" Success with error code: 1 – belltric Aug 15 '16 at 08:55
  • A few issues there. I'm not sure why the first line is failing just yet. The "Success with error code" message is because you need to do a `goto error ` not `goto success` – FloatingKiwi Aug 15 '16 at 09:01
  • What does your batch file echo when it runs that `cd %~dp0`. It should be the directory of the batch file itself, which should always exist. – FloatingKiwi Aug 15 '16 at 09:03
  • i know why first line failed as cd %~dp0 and ConsoleApp.exe must be separated into different line. I got the correct result now. Thanks for both of your help ^^ – belltric Aug 15 '16 at 09:06