VB6 创建控制台应用程序

时间:2021-08-17 20:58:42

' 功能:为VB程序创建一个consolewindow。
Private Declare Function AllocConsole Lib
"kernel32" () As Long

'功能:销毁为VB程序创建的consolewindow。
Private Declare
Function FreeConsole Lib "kernel32" () As
Long

' DOS程序有三个标准文件:标准输入文件(stdin),标准输出文件(stdout),标准错误文件(siderr)。与此类
'似,控制台程序窗口有三个句柄:
'  输入句柄(inputhandle)—指向控制台程序的输入缓冲区
'  输出句柄(outputhandle)、错误句柄(errorhandle)—指向控制台程序的屏幕输出缓冲区
'  在能够进行输入/输出操作之前,必须用GetstdHandle函数取得consolewindow的这三个句柄。
'功能:返回consolewindow的三个句柄之一。
Private
Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As
Long

'nStdHandle参数说明:
Private Const INPUT_HANDLE =
-10&  '返回inputhandle

Private Const OUTPUT_HANDLE = -11&
'返回outputhandle

Private Const ERROR_HANDLE =
-12&  '返回errorhandle

' consolewindow并获得其input/outputhandle后,就可以利用WriteConsole和ReadConsole进行输入/输出了。

'功能:   向控制台窗口输出字符串?
'说明:     hConsoleOutput—控制台的outputhandle?
'         
lpBuffer—要输出的字符串?
'         
nNumberOfCharsToWrite—要输出的字符串的长度?
'     lpNumberofCharsWritten—实际输出的字符串的长度,可置为vbNull。
'     lpReserved—保留,必须置为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

'功能:
从输入缓冲区输入字符串?
'    说明:此函数是以块方式输入信息。在本文的示例中,只有用户按了Enter(回车)键后,此函数才返回。
'      hConsoleInput—consolewindow的inputhandle?
'      lpBuffer—输入缓冲区地址?
'      nNumberOfCharsToRead—输入缓冲区的长度?
'    lpNumberOfCharsRead—实际读入的字符数,可置为vbNull。
'    lpReserved—保留,必须置为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

'功能:   设置控制台输入缓冲区的输人模式或屏幕输出缓冲区的输出模式?
'     
说明:在用ReadConsole和WriteConsole函数行输入/输出前,要用此函数设置好输入/输出模式。
'        hConsoleHandle—consolewindow的Inputhandle或outputhandle
Private
Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As Long,
ByVal dwMode As Long) As
Long

'dwMode是要设置的输入或输出模式值。hConsoleHandle是Inputhandle时,dwMode可取如下值的组合:

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