Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return exit code after running an exe?

Tags:

.net

vbscript

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?

like image 546
Jameel Moideen Avatar asked Dec 18 '12 05:12

Jameel Moideen


People also ask

How do I find exit code for Exe?

Exit Code Of Windowed Application # Windows CMD C:\> start /wait app.exe C:\> if %ErrorLevel% equ 0 (echo True) else (echo False) # Windows PowerShell PS C:\> app.exe PS C:\> $?

How do I return a powershell code?

Use the command Exit $LASTEXITCODE at the end of the powershell script to return the error codes from the powershell script. $LASTEXITCODE holds the last error code in the powershell script.

What is process exit code?

Each process has an exit code that can be accessed by other processes and can be used to indicate success or failure. A process sets an exit code in the value returned from either WinMain or the "C" main function. A process could, for example, return 0 to indicate success or a non-zero value indicating an error code.


2 Answers

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

like image 63
Paul Farry Avatar answered Sep 28 '22 19:09

Paul Farry


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
like image 24
gideon Avatar answered Sep 28 '22 19:09

gideon