16

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?

Deanna
  • 23,876
  • 7
  • 71
  • 156
CodeBlue
  • 14,631
  • 33
  • 94
  • 132

4 Answers4

26

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

gbianchi
  • 2,129
  • 27
  • 35
12

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.

Bob77
  • 13,167
  • 1
  • 29
  • 37
  • 3
    +1: Nice one. So, just like I said then, easy when you know how :) – Binary Worrier May 10 '12 at 21:41
  • Doesn't work for me. I included the Microsoft Scripting Runtime. It compiles and runs, it just doesn't print anything to the dos prompt which I'm running it from. Perhaps it doesn't work in Windows 10? – Developer Webs Oct 01 '20 at 13:15
4

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

MarkJ
  • 30,070
  • 5
  • 68
  • 111
0

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.

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • Actually it can be done quite trivially from VB6 using the FSO. Of course it only applies when you *have* a console, which you seldom do in a VB6 program - unless you AllocConsole() or are running as an EXE relinked for the Console Subsystem. But I agree it isn't the best tool for debugging. – Bob77 May 09 '12 at 17:20
  • @Bob: So, comes under the heading of "Easy when you know how" maybe? :) – Binary Worrier May 10 '12 at 08:48
  • Not much to it. See the extraneous answer I posted. – Bob77 May 10 '12 at 17:40