要連結你可以將這些行添加到您的.vbp文件的末尾控制檯子系統:
[VBCompiler]
LinkSwitches=/SUBSYSTEM:CONSOLE
然後編譯EXE應該可以正常工作。
也可以考慮一下這個示例程序。這裏是Module1.bas:
Option Explicit
Private StdIn As Scripting.TextStream
Private StdOut As Scripting.TextStream
'--- Only required for testing in IDE or Windows Subsystem ===
Private Declare Function AllocConsole Lib "kernel32"() As Long
Private Declare Function GetConsoleTitle Lib "kernel32" _
Alias "GetConsoleTitleA" (_
ByVal lpConsoleTitle As String, _
ByVal nSize As Long) As Long
Private Declare Function FreeConsole Lib "kernel32"() As Long
Private mEnableConsole As Boolean
Private mAllocated As Boolean
Private Property Get EnableConsole() As Boolean
EnableConsole = mEnableConsole
End Property
Private Property Let EnableConsole(ByVal RHS As Boolean)
Dim Title As String
If Not RHS And mEnableConsole Then
If mAllocated Then
StdOut.Write "Press enter to continue..."
StdIn.ReadLine
FreeConsole
mAllocated = False
End If
mEnableConsole = False
ElseIf RHS And Not mEnableConsole Then
Title = Space$(260)
If GetConsoleTitle(Title, 260) = 0 Then
AllocConsole
mAllocated = True
End If
mEnableConsole = True
End If
End Property
'--- End testing ---------------------------------------------
Private Sub Main()
Dim Msg As String
EnableConsole = True
On Error GoTo Cleanup
With New Scripting.FileSystemObject
Set StdIn = .GetStandardStream(Scripting.StdIn)
Set StdOut = .GetStandardStream(Scripting.StdOut)
End With
With StdOut
.Write "Hello from "
.Write App.Path
.Write "\"
.Write App.EXEName
.WriteLine ".exe"
.Write "Command line: ["
.Write Command$()
.WriteLine "]"
.WriteLine "Type an input message and press Enter:"
Msg = StdIn.ReadLine()
.WriteLine "I saw:"
.WriteLine Msg
End With
Msg = 1/0 'Cause an exception for illustration!
Cleanup:
If Err Then
With StdOut
.Write "#Error: "
.Write CStr(Err.Number)
.Write ", "
.WriteLine Err.Description
End With
End If
EnableConsole = False
End Sub
這可以很方便,因爲我們大多數人喜歡能夠測試和調試。
爲了測試編譯的程序,你可以嘗試像TestProject1.cmd:
@Echo Off
Project1 Run from CMD file!
Pause
這假設您作爲當然的PROJECT1.EXE編譯。
您也可以製作StdIn
,StdOut
和EnableConsole
全部Public
以供其他模塊訪問。我可能會做只是EnableConsole
「吸氣劑」 Public
自己雖然。
看一看這個頁面。除了使用GetStandardStream之外,還有更多。 http://www.nirsoft.net/vb/console_application_visual_basic.html – Marc
大多數VB6程序都是針對Windows子系統進行編譯和鏈接的,在那裏沒有標準的I/O流可以使用。這很可能是事情出錯的地方。 – Bob77
@ Bob77希望你的意思是說運行** LINK.exe ** '「C:\ Program Files文件(x86)\ Microsoft Visual Studio \ VB98 \ LINK.EXE」/編輯/子系統:CONSOLE Application.exe' – Joby