' function: Create a Consolewindow for VB program.
Private Declare Function allocconsole Lib "kernel32" () as Long
' Function: Destroy the Consolewindow created for VB program.
Private Declare Function freeconsole Lib "kernel32" () as Long
The DOS program has three standard files: standard input file (stdin), standard output file (STDOUT), standard error file (Siderr). With this type of
' Like, the Console program window has three handles:
' Input handle (inputhandle)-the input buffer to the console program
' Output handle (outputhandle), error handle (errorhandle)-screen output buffer to the console program
The three handles of the Consolewindow must be obtained with the GetStdHandle function before the input/output operation can be performed.
' function: Returns one of the three handles of a consolewindow.
Private Declare Function getstdhandle Lib "kernel32" (ByVal Nstdhandle as long) as long
' Nstdhandle parameter Description:
Private Const input_handle = -10& ' return inputhandle
Private Const output_handle = -11& ' return outputhandle
Private Const error_handle = -12& ' return errorhandle
' Consolewindow and get their input/outputhandle, you can use Writeconsole and readconsole for input/output.
' Function: Output a string to the console window?
' Description: hconsoleoutput-console outputhandle?
' lpbuffer-the string to output?
' nnumberofcharstowrite-the length of the string to output?
' lpnumberofcharswritten-the length of the actual output string, which can be set to vbnull.
' lpreserved-reserved, must be placed as Vbnul.
Private Declare Function writeconsole Lib "kernel32" Alias "Writeconsolea" _
(ByVal Hconsoleoutput as Long, _
Lpbuffer as Any, _
ByVal Nnumberofcharstowrite as Long, _
Lpnumberofcharswritten as Long, _
Lpreserved as any) as Long
' function: input string from input buffer?
' Description: This function is to enter information in block mode. In the example in this article, this function returns only if the user presses the ENTER key.
' Hconsoleinput-consolewindow's inputhandle?
' lpbuffer-input buffer address?
' nnumberofcharstoread-the length of the input buffer?
' lpnumberofcharsread-the number of characters actually read, which can be set to vbnull.
' lpreserved-reserved, must be placed as vbnull.
Private Declare Function readconsole Lib "kernel32" Alias "Readconsolea" _
(ByVal Hconsoleinput as Long, _
Lpbuffer as Any, _
ByVal Nnumberofcharstoread as Long, _
Lpnumberofcharsread as Long, _
Lpreserved as any) as Long
' function: Set the output mode of the console input buffer in the sender mode or the screen output buffer?
' Description: Use this function to set the input/output mode before using the Readconsole and Writeconsole function line input/output.
' Hconsolehandle-consolewindow's Inputhandle or Outputhandle
Private Declare Function setconsolemode Lib "kernel32" (ByVal hconsolehandle as Long, ByVal Dwmode as long) as long
' Dwmode is the input or output mode value to be set. When Hconsolehandle is Inputhandle, Dwmode is preferable to a combination of the following values:
Private Const line_input = &h2
Private Const echo_input = &h4
Private Const mouse_input = &h10
Private Const processed = &h1
Sub Main ()
Call AllocConsole
Dim Inputhandle as Long
Dim Str as String
Dim Outputhandle as Long
str = "Hello World" & vbCrLf
Outputhandle = GetStdHandle (output_handle)
Call Writeconsole (Outputhandle, ByVal str, Len (str), vbnull, vbnull)
Inputhandle = GetStdHandle (input_handle)
Call Readconsole (Inputhandle, vbnull, 255, vbnull, vbnull)
Call Freeconsole
End Sub
VB6 Creating a console Application