Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB7
Expertise: beginner
Jun 22, 2002

Create console apps that return an exit code

Writing an application that returns an ERRORLEVEL to Dos is quite difficult in VB6, because you are forced to use a Windows API that ends the application immediately, thus preventing orderly release of resources. Conversely, this task is quite simple in VB.NET, thanks to the Exit static method of the Environment class.

' Terminate the current application and return ERRORLEVEL = 1
Environment.Exit(1)
As with any regular command-line utility, you can test the errorlevel in a batch file as follows

@ECHO OFF
REM call the app that can set the errorlevel
MyApp
REM higher errorlevels must be tested first
IF ERRORLEVEL 3 GOTO e3
IF ERRORLEVEL 2 GOTO e2
IF ERRORLEVEL 1 GOTO e1

REM Add here code for Errorlevel = 0
REM ...
GOTO End

:e1
REM Add here code for Errorlevel = 1
REM ...
GOTO End

:e2
REM Add here code for Errorlevel = 2
REM ...
GOTO End

:e3
REM Add here code for Errorlevel = 3
REM ...

:End

UPDATE: Richard Deeming and Burton Rodman wrote us to point at an alternative, and much cleaner way, to have your VB app return an exit code to the operating system. You just have to create a Function named Main, which returns an Integer, as in:

Function Main() As Integer
    ' ...
    ' Return ERRORLEVEL = 1
    Return 1
End Function
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date