Create console apps that return an exit code

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 = 1Environment.Exit(1)

As with any regular command-line utility, you can test the errorlevel in a batch file as follows

@ECHO OFFREM call the app that can set the errorlevelMyAppREM higher errorlevels must be tested firstIF ERRORLEVEL 3 GOTO e3IF ERRORLEVEL 2 GOTO e2IF ERRORLEVEL 1 GOTO e1REM Add here code for Errorlevel = 0REM ...GOTO End:e1REM Add here code for Errorlevel = 1REM ...GOTO End:e2REM Add here code for Errorlevel = 2REM ...GOTO End:e3REM Add here code for Errorlevel = 3REM ...: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 1End Function

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes