do anyone know if you can make a VB program act like a DOS terminal program.. ie in a DOS Shell.
Printable View
do anyone know if you can make a VB program act like a DOS terminal program.. ie in a DOS Shell.
I have tried some on my own, got commands like dir, cd, del and move to work.
Why don't you make it takes everything and place in a batch file. Once you have the batch program, just simply shell it.
So for example, if the user type in the multi-lines text box, just write it out to a batch file and shell it afterward.
Is that what you are talking about?
It's just a matter of API calls. Here's an exmaple. Put this code into a module. Make sure there is no form in the project and make sure the startup form is Sub Main.
Sorry about the code being a little uneasy to read. I tired to reformat it but I forgot to save my work and I didn't
feel like doing it again. :(
Anyway, here is the code.
Code:'+========================================================
'File: Console.bas
'
'Summary: Demonstrates how it is possible to create and
'manipulate a console box in Visual Basic.
'
'Classes: None
'
'Functions: Main, ConsolePrint, ConsoleRead
'
'Origin: Visual Basic 6.0 For Windows
'
'--------------------------------------------------------
'(C) Copyright 1999 by Scott Lloyd. All Rights Reserved.
'Scott Lloyd
'===========================================================+
Option Explicit
'''''D E C L A R A T I O N S''''''''''''''''''''''''''''''''''''
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
''''C O N S T A N T S'''''''''''''''''''''''''''''''''''''
'I/O handlers for the console window. These are much like the
'hWnd handlers to form windows.
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_ERROR_HANDLE = -12&
'Color values for SetConsoleTextAttribute.
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
'For SetConsoleMode (input)
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
'For SetConsoleMode (output)
Private Const ENABLE_PROCESSED_OUTPUT = &H1
Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2
'''''G L O B A L S'''''''''''''''''''''''''''''''''''
Private hConsoleIn As Long 'The console's input handle
Private hConsoleOut As Long 'The console's output handle
Private hConsoleErr As Long 'The console's error handle
'''''M A I N'''''''''''''''''''''''''''''''''''''''''
Private Sub Main()
Dim szUserInput As String
AllocConsole 'Create a console instance
SetConsoleTitle "VB Console Example" 'Set the title on the console window
'Get the console's handle
hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
hConsoleErr = GetStdHandle(STD_ERROR_HANDLE)
'Print the prompt to the user. Use the vbCrLf to get to a new line.
SetConsoleTextAttribute hConsoleOut, _
FOREGROUND_RED Or FOREGROUND_GREEN _
Or FOREGROUND_BLUE Or FOREGROUND_INTENSITY _
Or BACKGROUND_BLUE
ConsolePrint "VB Console Example" & vbCrLf
SetConsoleTextAttribute hConsoleOut, _
FOREGROUND_RED Or FOREGROUND_GREEN _
Or FOREGROUND_BLUE
ConsolePrint "Enter your name--> "
'Get the user's name
szUserInput = ConsoleRead()
If Not szUserInput = vbNullString Then
ConsolePrint "Hello, " & szUserInput & "!" & vbCrLf
Else
ConsolePrint "Hello, whoever you are!" & vbCrLf
End If
'End the program
ConsolePrint "Press enter to exit"
Call ConsoleRead
FreeConsole 'Destroy the console
End Sub
'''''F U N C T I O N S''''''''''''''''''''''''''''''''''
'F+F+++++++++++++++++++++++++++++++++++++++++++++++++++
'Function: ConsolePrint
'
'Summary: Prints the output of a string
'
'Args: String ConsolePrint
'The string to be printed to the console's ouput buffer.
'
'Returns: None
'
'-----------------------------------------------------
Private Sub ConsolePrint(szOut As String)
WriteConsole hConsoleOut, szOut, Len(szOut), vbNull, vbNull
End Sub
'F+F++++++++++++++++++++++++++++++++++++++++++++++++++++
'Function: ConsoleRead
'
'Summary: Gets a line of input from the user.
'
'Args: None
'
'Returns: String ConsoleRead
'The line of input from the user.
'---------------------------------------------------F-F
Private Function ConsoleRead() As String
Dim sUserInput As String * 256
Call ReadConsole(hConsoleIn, sUserInput, Len(sUserInput), vbNull, vbNull)
'Trim off the NULL charactors and the CRLF.
ConsoleRead = Left$(sUserInput, InStr(sUserInput, Chr$(0)) - 3)
End Function
Aaah I see Megatron.. Just what I needed. =)
Thanks everyone for the help!
Megatron!
That is pretty cool.
I think you might have forgot the forward slash in and that is why the codes out like that. I could be wrong but thanks for the code any way.
[Edited by Nitro on 05-12-2000 at 06:30 PM]
Very cool! but the refresh is all messed. try dragging the window around while it's waiting for input, no freshy-refresh. Is there a more friendly way of doing the reads?
Hmmm...I noticed this too. I'll look in to fixing it and I will post it ASAP.
Meg, Thanx that was a good one, i have modified it somewhat for easier use. I made a classmodule to do the work and leaved submain in the module.
Save this one as a classmodule:
While the main sub looks as cool as this:Code:VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clsConsole"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
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 ENABLE_PROCESSED_INPUT = 1&
Private Const ENABLE_LINE_INPUT = 2&
Private Const ENABLE_ECHO_INPUT = 4&
Private Const ENABLE_WINDOW_INPUT = 8&
Private Const ENABLE_MOUSE_INPUT = 16&
Private Const ENABLE_PROCESSED_OUTPUT = 1&
Private Const ENABLE_WRAP_AT_EOL_OUTPUT = 2&
Private hConsoleIn& 'The console's input handle
Private hConsoleOut& 'The console's output handle
Private hConsoleErr& 'The console's error handle
Property Get NextIO$()
Attribute NextIO.VB_UserMemId = 0
Dim sUserInput As String * 256
Call ReadConsole(hConsoleIn, sUserInput, Len(sUserInput), vbNull, vbNull)
NextIO = Left(sUserInput, InStr(sUserInput, Chr(0)) - 3)
End Property
Property Let NextIO(newvalue$)
WriteConsole hConsoleOut, newvalue, Len(newvalue), vbNull, vbNull
End Property
Property Let Title(newtitle$)
SetConsoleTitle newtitle
End Property
Sub Colors(QBforecolor, QBbackcolor)
SetConsoleTextAttribute hConsoleOut, QBforecolor + QBbackcolor * 16
End Sub
Private Sub Class_Initialize()
AllocConsole 'Create a console instance
hConsoleIn = GetStdHandle(-10) 'Get the console's INPUT handle
hConsoleOut = GetStdHandle(-11) 'OUTPUT
hConsoleErr = GetStdHandle(-12) 'ERROR
End Sub
Private Sub Class_Terminate()
FreeConsole 'Destroy the console
End Sub
Code:'''''M A I N'''''''''''''''''''''''''''''''''''''''''
Private Sub Main()
Dim szUserInput$
Dim C As New clsConsole
C.Title = "VB Console Example" 'Set the title on the console window
C.Colors 4, 14
C = "VB Console Example" & vbCrLf
C.Colors 0, 15
C = "Enter your name--> "
'Get the user's name
szUserInput = C
If Not szUserInput = vbNullString Then
C = "Hello, " & szUserInput & "!" & vbCrLf
Else
C = "Hello, whoever you are!" & vbCrLf
End If
'End the program
C = "Press enter to exit"
szUserInput = C
Set C = Nothing
End Sub