February 28, 2021
Hot Topics:

Writing Console-Mode Applications in Visual Basic

  • By Scott Lloyd
  • Send Email »
  • More Articles »

Visual Basic, as was mentioned earlier, has no built-in support for console work of any kind. This means that all of our interfacing with the console will be done through calls to the Win32 API. Our application has no attachment to the console box. It's not a resource of any kind, it's a process that we request the operating system to allocate for us. This is done by a call to the API function AllocConsole, whose prototype is as follows:

Private Declare Function _
AllocConsole Lib "kernel32" () As Long

This will create a console window in which we can print and receive input. This console window - this is important to remember - is a process controlled by the operating system. Think of all operations that we perform on it as "requests" (and these requests can be denied). Since it is a process, it must always be destroyed when we are done with it, or before the application terminates. We destroy the console process with the FreeConsole function.

Private Declare Function _
FreeConsole Lib "kernel32" () As Long

With this done, one can already write a program that brings up a console box and closes it:

Private Declare Function _
AllocConsole Lib "kernel32" () As Long

Private Declare Function _
FreeConsole Lib "kernel32" () As Long

Private Sub Main()
End Sub

Page 2 of 7

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

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