How to write to a debug console in VB6?

馋奶兔 提交于 2020-01-01 04:26:10

问题


I am new to VB. I want to test some old VB code, but I need the ability to print to the console to be able to test certain values that are set in the code. How to print to the console from VB?


回答1:


Use debug.print. But there is no console on a VB6 application, that will print to the debug window.




回答2:


This isn't expected to be the accepted answer because Debug.Print is the way to go for IDE testing.

However just to show how to use the standard I/O streams easily in VB6:

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

Note that very little of the code there is required for an actual Console program in VB6. The bulk of it is about allocating a Console window when the program is not running in the Console Subsystem.




回答3:


Use OutputDebugString and view the messages with the excellent free DebugView. More information and reusable code from Karl Peterson here




回答4:


This isn't something that Vb6 can easily do (I'm sure it can be done, but you'd be calling native Win32 APIs, and isn't worth the pain if you're just using it for debugging)

Your best bet (IMHO) is to write those values to a log file.



来源:https://stackoverflow.com/questions/10517338/how-to-write-to-a-debug-console-in-vb6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!