Author Topic: [VB6] Using console output  (Read 4802 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[VB6] Using console output
« on: November 21, 2009, 11:33:43 pm »
Again, found this sorting through old files. Stick this in a class file.

Code: [Select]
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsWinConsole"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' =============================================================================
'    Copyright (c) 2002 Richard Caetano - http://www.stronglytyped.com
'
'    Permission is hereby granted, free of charge, to any person obtaining a
'    copy of this software and associated documentation files (the "Software"),
'    to deal in the Software without restriction, including without limitation
'    the rights to use, copy, modify, merge, publish, distribute, sublicense,
'    and/or sell copies of the Software, and to permit persons to whom the
'    Software is furnished to do so, subject to the following conditions:
'
'    The above copyright notice and this permission notice shall be included
'    in all copies or substantial portions of the Software.
'
'    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
'    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
'    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
'    THE SOFTWARE.
'
'
'    clsWinConsole - A Visual Basic class that encapsulates calls to the Win32
'                    API for interacting with the Console
'
'
' =============================================================================


Option Explicit

Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Private Declare Function ReadConsole Lib "kernel32" Alias "ReadConsoleA" (ByVal hConsoleInput As Long, ByVal lpBuffer As String, ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, lpReserved As Any) As Long
Private Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleOutput As Long, dwMode As Long) As Long
Private Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Private Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_ERROR_HANDLE = -12&
Private Const FOREGROUND_BLUE = &H1
Private Const FOREGROUND_GREEN = &H2
Private Const FOREGROUND_RED = &H4
Private Const FOREGROUND_INTENSITY = &H8
Private Const BACKGROUND_BLUE = &H10
Private Const BACKGROUND_GREEN = &H20
Private Const BACKGROUND_RED = &H40
Private Const BACKGROUND_INTENSITY = &H80
Private Const ENABLE_LINE_INPUT = &H2
Private Const ENABLE_ECHO_INPUT = &H4
Private Const ENABLE_MOUSE_INPUT = &H10
Private Const ENABLE_PROCESSED_INPUT = &H1
Private Const ENABLE_WINDOW_INPUT = &H8
Private Const ENABLE_PROCESSED_OUTPUT = &H1
Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2
Private hConsoleIn As Long
Private hConsoleOut As Long
Private hConsoleErr As Long

Private Sub Class_Terminate()
    Hide
End Sub

'Show the console window
Public Sub Show()
    Call AllocConsole
    Call SetConsoleTitle("JoeBot v" & App.Major & " revision " & App.Minor & " build " & App.Revision & " by Joe[x86]")
    Let hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
    Let hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
    Let hConsoleErr = GetStdHandle(STD_ERROR_HANDLE)
End Sub

' If the console window is active then free it
Public Sub Hide()
    If Active Then FreeConsole
End Sub

'Returns True if the console is open
Public Function Active() As Boolean
    Active = (hConsoleIn <> 0) Or (hConsoleOut <> 0) Or (hConsoleErr <> 0)
End Function

'Sends the text out to the console
Public Sub WriteText(text As String)
    Call WriteConsole(hConsoleOut, text, Len(text), vbNull, vbNull)
End Sub

'Sends the text out to the console
Public Sub WriteTextLine(ByVal text As String)
    Call WriteConsole(hConsoleOut, text & vbNewLine, Len(text & vbNewLine), vbNull, vbNull)
End Sub

'Prompts the user for input
Public Function ReadText() As String
    DoEvents
    Dim sUserInput As String * 256
    Call ReadConsole(hConsoleIn, sUserInput, Len(sUserInput), vbNull, vbNull)
    Let ReadText = Left$(sUserInput, InStr(sUserInput, Chr$(0)) - 3)
End Function
I'd personally do as Joe suggests

You might be right about that, Joe.