Returning exit codes from windows forms application

Z

Zeno Lee

What is the best way to return an exit code from a VB.NET windows forms app?

My Forms application is dual purpose. It is an interactive windows app. It
is also automated and run via a script and it needs to return an exit code
to that script.

I've tried a few ways to return an exit code.

1. As a forms app with Entry point being Form1, I've tried
Application.Exit(999). It does not return the exit code.

2. As a forms app with
Shared Sub Main() As Integer
Application.Run(New Form1)
Return 999
End Sub

This does not return the exit code.

3. I changed the app type to console and shared sub main() as integer.
This returns an exit code. However, when run interactively, there's always
a console window running at the same time as the form, which is annoying.


In C# a windows forms app actually returns an exit code.
[STAThread]
static void Main()
{
Application.Run(new Login());
return 999;
}


How can I make a VB.NET windows form app that returns an exit code?
 
Z

Zeno Lee

Minor correction to the post in #2

Shared Function Main() As Integer
Application.Run(New Form1)
Return 999
End Sub
 
H

Herfried K. Wagner [MVP]

Zeno Lee said:
is also automated and run via a script and it needs to return an exit code
to that script.

I've tried a few ways to return an exit code.

1. As a forms app with Entry point being Form1, I've tried
Application.Exit(999). It does not return the exit code.

2. As a forms app with
Shared Sub Main() As Integer
Application.Run(New Form1)
Return 999
End Sub

This does not return the exit code.

Did you check the exit code in a batch file, or by starting the process
using 'Process.Start' and examining the 'Process' object's 'ExitCode'?

\\\
Imports System.Diagnostics
..
..
..
Dim p As Process = Process.Start("C:\Test.exe")
p.WaitForExit()
MsgBox(CStr(p.ExitCode))
p.Dispose()
///

The exit code shown in the IDE's output window is wrong!
 
Z

Zeno Lee

I am checking the %errorlevel% variable from the console. As far as I know,
that is accurate.
 
H

Herfried K. Wagner [MVP]

Zeno Lee said:
I am checking the %errorlevel% variable from the console. As far as I
know, that is accurate.

Can you post the (batch?) code you use to check it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top