BASIC Developer & Support Resources > VB (5/6/CE) Classic

VB6 Console Applications

<< < (2/3) > >>

I found a good article to help get you going with VB6 console applications.

Actually I would like this to use the same console window I started the program in. Like a true console application.

--- Quote ---Key Statement: So, start a new Standard EXE project and remove the default form (Form1) from the project. Add a module and you're set. That one module is the only component that is needed in a console project, just like a windowless project.

--- End quote ---

--- Code: Visual Basic ---'+================================================' 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 = &H1Private Const FOREGROUND_GREEN = &H2Private Const FOREGROUND_RED = &H4Private Const FOREGROUND_INTENSITY = &H8Private Const BACKGROUND_BLUE = &H10Private Const BACKGROUND_GREEN = &H20Private Const BACKGROUND_RED = &H40Private Const BACKGROUND_INTENSITY = &H80 'For SetConsoleMode (input)Private Const ENABLE_LINE_INPUT = &H2Private Const ENABLE_ECHO_INPUT = &H4Private Const ENABLE_MOUSE_INPUT = &H10Private Const ENABLE_PROCESSED_INPUT = &H1Private Const ENABLE_WINDOW_INPUT = &H8'For SetConsoleMode (output)Private Const ENABLE_PROCESSED_OUTPUT = &H1Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2 '''''G L O B A L S'''''''''''''''''''''''''''''''''''Private hConsoleIn As Long ' The console's input handlePrivate hConsoleOut As Long ' The console's output handlePrivate 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 handlehConsoleIn = 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" & vbCrLfSetConsoleTextAttribute hConsoleOut, _FOREGROUND_RED Or FOREGROUND_GREEN _Or FOREGROUND_BLUEConsolePrint "Enter your name--> " 'Get the user's nameszUserInput = ConsoleRead()If Not szUserInput = vbNullString ThenConsolePrint "Hello, " & szUserInput & "!" & vbCrLfElseConsolePrint "Hello, whoever you are!" & vbCrLfEnd If 'End the programConsolePrint "Press enter to exit"Call ConsoleRead FreeConsole ' Destroy the consoleEnd 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, vbNullEnd 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 StringDim sUserInput As String * 256Call 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 
Writing Console-Mode Applications in Visual Basic

It seems I have gone full circle and the original console example post at the top of this thread is exactly what I needed. What makes this a better solution is it comes with a utility to change VB6 GUI .exe programs to look like a standard console applications. The second example has a few goodies I may include in the first example.

Mike Lobanovsky:
Now you can write colorful VB6 console submissions to my recent challenge on BP. ;)

As for Russian translation, hehe, this ain't no Russian Cyrillic alphabet you're seeing, that's the second half of ASCII table for Western Europe. :)

Actually not much meaningful info in there, just a usual blurb:

--- Code: Visual Basic ---' ******************************************************************************' *     Fire-Lines © 2013                                                      *' *     Module:                                                                *' *         EasyConsole                                                        *' *     Description:                                                           *' *         This module introduces revolutionary simple support                *' *         for console application development using VB.                      *' *     Author:                                                                *' *         Vladislav Petrovsky (firehacker)                                   *' *     Change Log:                                                            *' *         *   2013-01-19  firehacker  Original commit                        *' *                                                                            *' *     Current version: 1.0.0                                                 *' *                                                                            *' ****************************************************************************** 
What concerns zip downloads, you can't use them from a translated page. Google Translator tries to interpret the link rather than follow it. :D

Here is the link to the original Russian page. All the DLs seem to be working as expected. Refer to your PDF for what's in the links.

I'd still recommend using that approach. You'll have separate access to stdin, stdout and stderr (these are 3 different routes for reading from, writing to, and managing errors in, the Windows console). Dot notation is very VB-stylish. And you can always add more functionality, such as e.g. text coloration methods (i.e. commands), directly in the EasyConsole module.

Template installed as described in the PDF, open up a new EasyConsole-type project in the IDE, click the Object Browser toolbar button, select Scripting from the dropdown combobox, scroll down to the TextStream class and see what methods it has. That's what you can do with each of stdin, stdout and stderr on default.

Thanks Mike!

That worked and something I should have realized as obvious.

It looks like a couple more steps but it's more robust than the first example I posted in this thread.

Mike Lobanovsky:
The couple of required steps to take will imply patching a copy of your existing VB6.exe (IDE) as a mixed CLI/GUI application using EDITBIN. If you have problems with this, PM or e-mail me the copy and I'll do it for you.

Everything installed and patched, you will create a new desktop shortcut (symlink, hehe) for VB6CON.exe alongside your existing VB6.exe and launch it whenever you want to create/debug a console app. Once launched, it will look exactly as your original IDE where you can edit code and create VB forms. But when you add an EasyConsole template, it will also respawn a console window for interaction with you in the immediate mode and also as an output/input screen for running and debugging your current project interactively using your usual Run/Pause/Stop buttons, menus or hotkeys. Unpatched VB6 IDE will not allow you to pause/resume execution and will crash.

Templated VB6CON IDE will also enable you to automatically compile your project directly to a console exe that wouldn't require any additional patching or distribution of dependencies or manual editing of the project file. When run outside the command prompt, each one of such exes would respawn their own console window and close it automatically on exit, or they would write directly to the existing common console window or screen if launched from the command prompt.


[0] Message Index

[#] Next page

[*] Previous page

Go to full version