dcsimg
December 6, 2016
Hot Topics:

Writing Console-Mode Applications in Visual Basic

  • November 19, 2002
  • By Scott Lloyd
  • Send Email »
  • More Articles »

There is little point in writing a program that provides no means by which the user can interface with it. For simple input retrieval, the ReadConsole function can be used. This function is a blocking function, in other words it doesn't return until it has something to return. In the case of our example, which is in the final section of this article, the ReadConsole function will not return until the user presses enter.

Below are the declarations of two of the functions that we will need. First we must set the console's input buffer mode. This is done by a call to SetConsoleMode where we pass the input handle and a flag indicating which types of input will be recorded. Note that in the example we will be passing ENABLE_ECHO_INPUT to SetConsoleMode. To actually receive the user's input, we will be using ReadConsole. It is important to remember to use fixed-length strings when using this function. A good practice here is to fix the string to a length of 256 charactors. How to do this will be shown in the example at the end of the article.

IMPORTANT NOTE: Yet again, the Microsoft API Text Viewer has incorrect declarations. In the ReadConsole declaration, lpBuffer is a string passed by value, not an any-type reference pass.

Private Declare Function _
SetConsoleMode Lib "kernel32" _
(ByVal hConsoleOutput As Long, _
dwMode As Long) As Long

Private Declare Function _
ReadConsole Lib "kernel32" Alias _
"ReadConsoleA" (ByVal hConsoleInput As Long, _
ByVal lpBuffer As String, ByVal _
nNumberOfCharsToRead As Long, _
lpNumberOfCharsRead As Long, _
lpReserved As Any) As Long

'For SetConsoleMode (input)
Private Const ENABLE_LINE_INPUT = &H2
Private Const ENABLE_ECHO_INPUT = &H4
Private Const ENABLE_MOUSE_INPUT = &H10
Private Const ENABLE_PROCESSED_INPUT = &H1
Private Const ENABLE_WINDOW_INPUT = &H8
'For SetConsoleMode (output)
Private Const ENABLE_PROCESSED_OUTPUT = &H1
Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2

You don't always have to use SetConsoleMode before you receive input, but it is a good idea. If you start delving into the deaper and uglier depths of programming console apps, you'll need it.





Page 5 of 7



Comment and Contribute

 


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

 

 


Enterprise Development Update

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

Sitemap | Contact Us

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