Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

command line VB program 1

Status
Not open for further replies.

fechen

Technical User
Jan 25, 2002
48
US
Maybe this is a stupid question.

I want to make a command line program using VB.

I found I can use following as main entry point.
sub main()
'do whatever
end sub

but don't know how to print something out to the command line, just like "hello"

I want to use this program in my script to log the memory usage info. The script (with other scripts) usually will be running for >=24 hr.

 
Copy and paste this code into your module.

Note: It contains a sub main so be careful not to overwrite what you already have in your sub main

This contains a sample on how to create, read, write and close the console application.

Code:
Option Explicit
Private Const FOREGROUND_BLUE = &H1
Private Const FOREGROUND_GREEN = &H2
Private Const FOREGROUND_RED = &H4
Private Const BACKGROUND_BLUE = &H10
Private Const BACKGROUND_GREEN = &H20
Private Const BACKGROUND_RED = &H40
Private Const BACKGROUND_INTENSITY = &H80&
Private Const BACKGROUND_SEARCH = &H20&
Private Const FOREGROUND_INTENSITY = &H8&
Private Const FOREGROUND_SEARCH = (&H10&)
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 Const STD_OUTPUT_HANDLE = -11&
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_ERROR_HANDLE = -12&
Private Const INVALID_HANDLE_VALUE = -1&
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) 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 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 hConsoleOut As Long, hConsoleIn As Long, hConsoleErr As Long


Sub Main()

    Dim Age As String
    
    If AllocConsole() Then
        hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
        If hConsoleOut = INVALID_HANDLE_VALUE Then MsgBox "Unable to get STDOUT"
        hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
        If hConsoleOut = INVALID_HANDLE_VALUE Then MsgBox "Unable to get STDIN"
    Else
        MsgBox "Couldn't allocate console"
    End If

    SetConsoleTitle "Your title here"
    ConsoleWriteLine "Hello today is " & Date
    ConsoleWrite "Enter your age: "
    Age = ConsoleReadLine()
    ConsoleWriteLine "You are " & Age & "Years Old!"
    ConsoleWriteLine "Press Any Key to Quit!"
    ConsoleReadLine

    CloseHandle hConsoleOut
    CloseHandle hConsoleIn
    FreeConsole
End Sub
Sub ConsoleWriteLine(sInput As String)
     ConsoleWrite sInput + vbCrLf
End Sub
Sub ConsoleWrite(sInput As String)
     Dim cWritten As Long
     WriteConsole hConsoleOut, ByVal sInput, Len(sInput), cWritten, ByVal 0&
End Sub
Function ConsoleReadLine() As String
    Dim ZeroPos As Long
    'Create a buffer
    ConsoleReadLine = String(10, 0)
    'Read the input
    ReadConsole hConsoleIn, ConsoleReadLine, Len(ConsoleReadLine), vbNull, vbNull
    'Strip off trailing vbCrLf and Chr$(0)'s
    ZeroPos = InStr(ConsoleReadLine, Chr$(0))
    If ZeroPos > 0 Then ConsoleReadLine = Left$(ConsoleReadLine, ZeroPos - 3)
End Function
 
And here's the short version:
[tt]
Option Explicit

Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long

Public Sub main()
Dim fso As Object
Dim stdin As Object
Dim stdout As Object
Dim Age As String

Set fso = CreateObject("Scripting.FileSystemObject")
Set stdin = fso.GetStandardStream(0)
Set stdout = fso.GetStandardStream(1)
stdout.WriteLine "Hello today is " & Date
stdout.Write "Enter your age: "
Age = stdin.ReadLine
stdout.WriteLine "You are " & Age & " Years Old!"
stdout.WriteLine "Press Enter to quit!"
stdin.ReadLine
FreeConsole
End Sub
 
You forgot to add the call to AllocConsole before you call the GetStandardStream Function

:)
 
Quite so - don't know how that happened. Slap on wrist time...
 
Thanks, guys.

but strongm, your solution doesn't work on my machine. It compiled, but when I run it, error message says method "WriteLine" is not define for Object "iTextStream", which I guess is the type of stdin/stdout.

Could you let me know what dll/software/whatever is required, and if possible, where can I find a good reference. My Visual studio help doc even doesn't have "GetStandardStream" :-(

thanks,
Feng
 
Yes, you need to put an AllocConsole call in, as noted by [b}bjd4jc[/b]. i.e:
[tt]
Option Explicit

Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long

Public Sub main()
Dim fso As Object
Dim stdin As Object
Dim stdout As Object
Dim Age As String

AllocConsole
Set fso = CreateObject("Scripting.FileSystemObject")
Set stdin = fso.GetStandardStream(0)
Set stdout = fso.GetStandardStream(1)
stdout.WriteLine "Hello today is " & Date
stdout.Write "Enter your age: "
Age = stdin.ReadLine
stdout.WriteLine "You are " & Age & " Years Old!"
stdout.WriteLine "Press Enter to quit!"
stdin.ReadLine
FreeConsole
End Sub
 
I should add that the reason you get the error message is that VB doesn't actually have any standard streams, since they get removed during the VB startup. Using AllocConsole gives them back...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top