如何在VB6中写入调试控制台?

问题描述 投票:11回答:4

我是VB的新手。我想测试一些旧的VB代码,但我需要能够打印到控制台,以便能够测试代码中设置的某些值。如何从VB打印到控制台?

debugging vb6 console
4个回答
21
投票

使用debug.print。但是VB6应用程序上没有控制台,它将打印到调试窗口。


11
投票

预计这不会是公认的答案,因为Debug.Print是进行IDE测试的方法。

但是,为了展示如何在VB6中轻松使用标准I / O流:

Option Explicit
'
'Reference to Microsoft Scripting Runtime.
'

Public SIn As Scripting.TextStream
Public SOut 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 Allocated As Boolean

Private Sub Setup()
    Dim Title As String

    Title = Space$(260)
    If GetConsoleTitle(Title, 260) = 0 Then
        AllocConsole
        Allocated = True
    End If
End Sub

Private Sub TearDown()
    If Allocated Then
        SOut.Write "Press enter to continue..."
        SIn.ReadLine
        FreeConsole
    End If
End Sub
'--- End testing ---------------------------------------------

Private Sub Main()
    Setup 'Omit for Console Subsystem.

    With New Scripting.FileSystemObject
        Set SIn = .GetStandardStream(StdIn)
        Set SOut = .GetStandardStream(StdOut)
    End With

    SOut.WriteLine "Any output you want"
    SOut.WriteLine "Goes here"

    TearDown 'Omit for Console Subsystem.
End Sub

请注意,VB6中的实际Console程序只需要很少的代码。其中大部分是在程序未在控制台子系统中运行时分配控制台窗口。


3
投票

使用OutputDebugString并使用优秀的免费DebugView查看消息。来自Karl Peterson here的更多信息和可重复使用的代码


0
投票

这不是Vb6可以轻松完成的事情(我确信它可以完成,但是你要调用原生的Win32 API,如果你只是用它来调试它就不值得痛苦)

你最好的选择(恕我直言)是将这些值写入日志文件。

© www.soinside.com 2019 - 2024. All rights reserved.