How to return exit code after running exe?

I created a console application to test the function, and I need to run this application using vbscript. After executing this exe, I want to return the exit code whether the function will return success or not. How can I return a status or exit code to .net?

+7
source share
4 answers

I am going to assume that you are writing either C # or VB.NET. In any case, usually people have a Main function that returns nothing , but you can change this to return an integer to represent the exit code.

For C #, see this MSDN page .

You can do:

static int Main() { //... return 0; } 

For VB.NET, see this MSDN page .

You can do:

 Module mainModule Function Main() As Integer '.... '.... Return returnValue End Function End Module 
+8
source

In addition to @gideon, you can also install

 Environment.ExitCode = theExitCode; 

In other parts of your code, and go straight if something really bad happened.

+4
source

As @gideon pointed out, in your executable you must use the return to return the number.

In the script, please read %ERRORLEVEL% after calling this executable. What a Windows place contains a return code.

0
source

Given this C # program:

 class MainReturnValTest { static int Main(string[] args) { int rv = 0; if (1 == args.Length) { try { rv = int.Parse(args[0]); } catch(System.FormatException e) { System.Console.WriteLine("bingo: '{1}' - {0}", e.Message, args[0]); rv = 1234; } } System.Console.WriteLine("check returns {0}.", rv); return rv; } } 

Run Examples:

 check.exe check returns 0. check.exe 15 check returns 15. check.exe nonum bingo: 'nonum' Input string was not in a correct format. check returns 1234. 

and this VBScript script (minimized, do not do this during production):

 Option Explicit Const WshFinished = 1 Dim goWSH : Set goWSH = CreateObject("WScript.Shell") Dim sCmd : sCmd = "..\cs\check.exe" If 1 = WScript.Arguments.Count Then sCmd = sCmd & " " & WScript.Arguments(0) WScript.Echo sCmd Dim nRet : nRet = goWSH.Run(sCmd, 0, True) WScript.Echo WScript.ScriptName, "would return", nRet With goWSH.Exec(sCmd) Do Until .Status = WshFinished : Loop WScript.Echo "stdout of check.exe ==>" & vbCrLf, .Stdout.ReadAll() nRet = .ExitCode WScript.Echo ".ExitCode of check.exe", nRet End With ' !! http://stackoverflow.com/questions/2042558/how-do-i-get-the-errorlevel-variable-set-by-a-command-line-scanner-in-my-c-sha WScript.Echo "Errorlevel:", Join(Array(goWSH.Environment("PROCESS")("ERRORLEVEL"), goWSH.ExpandEnvironmentStrings("%ERRORLEVEL%"), "???"), " - ") WScript.Echo WScript.ScriptName, "returns", nRet WScript.Quit nRet 

Run Examples:

 cscript 13921064.vbs ..\cs\check.exe 13921064.vbs would return 0 stdout of check.exe ==> check returns 0. .ExitCode of check.exe 0 Errorlevel: - %ERRORLEVEL% - ??? <=== surprise, surprise 13921064.vbs returns 0 echo %ERRORLEVEL% 0 cscript 13921064.vbs nonum & echo %ERRORLEVEL% ..\cs\check.exe nonum 13921064.vbs would return 1234 stdout of check.exe ==> bingo: 'nonum' Input string was not in a correct format. check returns 1234. .ExitCode of check.exe 1234 Errorlevel: - %ERRORLEVEL% - ??? 13921064.vbs returns 1234 0 <=== surprise, surprise DNV35 E:\trials\SoTrials\answers\13927081\vbs echo %ERRORLEVEL% 1234 

You will see

  • WScript.Quit is a way to return the exit code from a script
  • You start another process using .Run or .Exec
  • .Run returns the exit code of the called process
  • .Exec installs .ExitCode (upon completion!)
  • Access to% ERRORLEVEL% in your script is useless (@LexLi)
  • cscript 13921064.vbs nonum & echo %ERRORLEVEL% also useless
0
source

All Articles