How to return exit code after running an exe?

2020-06-16 01:33发布

I have created a Console application for validating a function and this application i need to execute by using vbscript. After executing this exe i want to return an exit code whether the function return success or not. How can i return a status or exit code in .net?

标签: .net vbscript
4条回答
虎瘦雄心在
2楼-- · 2020-06-16 02:16

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;
    }
}

Sample runs:

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 (reduced to the bare minimum, don't do this in 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

Sample runs:

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'll see

  • WScript.Quit is the way to return an exit code from your script
  • You start another process by using .Run or .Exec
  • .Run returns the exit code of the called process
  • .Exec sets .ExitCode (after termination!)
  • Accessing %ERRORLEVEL% in your script is futile (@LexLi)
  • cscript 13921064.vbs nonum & echo %ERRORLEVEL% is useless too
查看更多
Bombasti
3楼-- · 2020-06-16 02:22

I'm going to assume you're writing either C# or VB.NET. In either 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
查看更多
啃猪蹄的小仙女
4楼-- · 2020-06-16 02:35

As @gideon commented, in your executable you must use return statement to return the number.

In your script, please read %ERRORLEVEL% after calling this executable. That's the place Windows holds the return code.

查看更多
Evening l夕情丶
5楼-- · 2020-06-16 02:39

In addition to @gideon you can also set

Environment.ExitCode = theExitCode;

In other parts of your code and exit directly if something really bad has happened

查看更多
登录 后发表回答