Paste the code attached below in a standard module:
______________________________________________________________________
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () 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, lpBuffer As Any, _
ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, _
lpReserved As Any) As Long
Private Declare Function GetStdHandle Lib "kernel32" ( _
ByVal nStdHandle As Long) As Long
Private Const STD_ERROR_HANDLE = -12&
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_OUTPUT_HANDLE = -11&
Private hOutput As Long
Private hInput As Long
Private hError As Long
Function ConsoleAlloc() As Boolean
AllocConsole
hOutput = GetStdHandle(STD_OUTPUT_HANDLE)
hInput = GetStdHandle(STD_INPUT_HANDLE)
hError = GetStdHandle(STD_ERROR_HANDLE)
ConsoleAlloc = ((hOutput <> 0) And (hInput <> 0) And ( _
hError <> 0))
End Function
Function ConsoleFree()
Dim lResult As Long
lResult = FreeConsole()
ConsoleFree = (lResult <> 0)
End Function
Function ConsoleWrite(sOutput As String) As Boolean
Dim lOutput As Long
Dim lWritten As Long
Dim lResult As Long
lOutput = Len(sOutput)
lResult = WriteConsole(hOutput, ByVal sOutput, lOutput, lWritten, _
0)
ConsoleWrite = (lResult <> 0)
End Function
Function ConsoleWriteLn(sOutput As String) As Boolean
ConsoleWriteLn = ConsoleWrite(sOutput & vbCrLf)
End Function
Function ConsoleError(sOutput As String) As Boolean
Dim lOutput As Long
Dim lWritten As Long
Dim lResult As Long
lOutput = Len(sOutput)
lResult = WriteConsole(hError, ByVal sOutput, lOutput, lWritten, _
0)
ConsoleError = (lResult <> 0)
End Function
Function ConsoleErrorLn(sOutput As String) As Boolean
ConsoleErrorLn = ConsoleError(sOutput & vbCrLf)
End Function
Function ConsoleRead(sInput As String) As Boolean
Dim lInput As Long
Dim lResult As Long
Dim lRead As Long
sInput = Space(255)
lInput = Len(sInput)
lResult = ReadConsole(hInput, ByVal sInput, lInput, lRead, 0)
If lRead > 1 Then sInput = Left(sInput, lRead - 2)
ConsoleRead = (lResult <> 0)
End Function
______________________________________________________________________
In your application, start by calling ConsoleAlloc.
Then, to write something to the console use ConsoleWrite or ConsoleWriteLn.
To get user input use ConsoleRead.
To write error messages use ConsoleError or ConsoleErrorLn.
When run from a command prompt, the application will allocate a new console
window.
This is because the VB Linker does not set the console subsystem flag in the
exe file header.
To do this you can either use some third party tool, a binary editor, or -
if you have
Visual C++ - use the EDITBIN utility.
HTH
--
Björn Holmgren
Guide Konsult AB
Post by ganHi a simple question here. I know "Command" is for us to read something from
DOS. How about if I want to print some output to the console?
Thanks in advance!