1. 创建一个EXE程序,加入一个Module1。然后在Module1中加入以下代码:
-----------------------------------------------------------
Option ExplicitDeclare Function AllocConsole Lib "kernel32" () As Long
Declare Function FreeConsole Lib "kernel32" () As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) _
        As Long
Declare Function GetStdHandle Lib "kernel32" (ByVal _
        nStdHandle As Long) As Long
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
Public Const STD_OUTPUT_HANDLE = -11&
-----------------------------------------------------------2.在Form1中加入一个Button1,然后加入以下代码:
-----------------------------------------------------------
Dim hConsole As LongPrivate Sub Command1_Click()
    Dim Result As Long, sOut As String, cWritten As Long
    
    sOut = "Hi There" & vbCrLf
    Result = WriteConsole(hConsole, ByVal sOut, Len(sOut), cWritten, _
                          ByVal 0&)
End SubPrivate Sub Form_Load()
    If AllocConsole() Then
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE)
        If hConsole = 0 Then MsgBox "Couldn't allocate STDOUT"
    Else
        MsgBox "Couldn't allocate console"
    End If
End SubPrivate Sub Form_Unload(Cancel As Integer)
    CloseHandle hConsole
    FreeConsole
End Sub
-----------------------------------------------------------3.编译成EXE程序,并执行EXE程序后点击Button,即可看到DOS命令窗口中出现"Hi There"。至于如何实现没有Form标准输入输出,你可在上述的程序基础上做些扩展。