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

VB6 Console Applications

(1/3) > >>

John:
I would like to use VB6 to create non-GUI console applications and it seems this is easy to do. My main goal with this thread is accessing COM/OLE functionality in a console application which could be used in a batch file or task scheduler. I stumbled onto an example of how to create a VB6 console application and a utility to change the mode of the .exe program. (GUI or Console) I'm not sure if it will work with non-VB6 programs but I'll give it a try by making a Script BASIC standalone console script into a headless Windows applications.




--- Code: Visual Basic ---Attribute VB_Name = "Module1"Option Explicit 'Console Application Sample'Written by Nir Sofer.''Web site: http://nirsoft.mirrorz.com''In order to create a console application from this code, follow the instructions below:'1. Make an executable file from this project.'2. Run the "Application Mode Changer" utility and change the mode of the'executable file to "Console Application". Public Declare Function GetStdHandle Lib "kernel32" _(ByVal nStdHandle As Long) As Long Private Declare Function WriteFile Lib "kernel32" _(ByVal hFile As Long, _lpBuffer As Any, _ByVal nNumberOfBytesToWrite As Long, _lpNumberOfBytesWritten As Long, _lpOverlapped As Any) As Long Public Const STD_OUTPUT_HANDLE = -11& Private Type COORD        x As Integer        y As IntegerEnd Type Private Type SMALL_RECT        Left As Integer        Top As Integer        Right As Integer        Bottom As IntegerEnd Type Private Type CONSOLE_SCREEN_BUFFER_INFO        dwSize As COORD        dwCursorPosition As COORD        wAttributes As Integer        srWindow As SMALL_RECT        dwMaximumWindowSize As COORDEnd TypePrivate Declare Function GetConsoleScreenBufferInfo Lib "kernel32" _(ByVal hConsoleOutput As Long, _lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Long Private Declare Function SetConsoleTextAttribute Lib "kernel32" _(ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long Private Const FOREGROUND_BLUE = &H1     '  text color contains blue.Private Const FOREGROUND_GREEN = &H2     '  text color contains green.Private Const FOREGROUND_INTENSITY = &H8     '  text color is intensified.Private Const FOREGROUND_RED = &H4     '  text color contains red. Private hOutput             As Long 'The following function writes the content of sText variable into the console window:Private Function WriteToConsole(sText As String) As Boolean    Dim lWritten            As Long        If WriteFile(hOutput, ByVal sText, Len(sText), lWritten, ByVal 0) = 0 Then        WriteToConsole = False    Else        WriteToConsole = True    End IfEnd Function Public Sub Main()    Dim scrbuf      As CONSOLE_SCREEN_BUFFER_INFO        'Get the standard output handle    hOutput = GetStdHandle(STD_OUTPUT_HANDLE)    GetConsoleScreenBufferInfo hOutput, scrbuf    WriteToConsole "Console Application Example In Visual Basic." & vbCrLf    WriteToConsole "Written by Nir Sofer" & vbCrLf    WriteToConsole "Web site: http://nirsoft.mirrorz.com" & vbCrLf & vbCrLf        'Change the text color to blue    SetConsoleTextAttribute hOutput, FOREGROUND_BLUE Or FOREGROUND_INTENSITY    WriteToConsole "Blue Color !!" & vbCrLf        'Change the text color to yellow    SetConsoleTextAttribute hOutput, FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_INTENSITY    WriteToConsole "Yellow Color !!" & vbCrLf        'Restore the previous text attributes.    SetConsoleTextAttribute hOutput, scrbuf.wAttributes    If Len(Command$) <> 0 Then        'Show the command line parameters:        WriteToConsole vbCrLf & "Command Line Parameters: " & Command$ & vbCrLf    End IfEnd Sub 
Project Web Site

Microsoft reference for a VB6 Console

Zip contains all source and executables for Windows 32 bit.

Mike Lobanovsky:
Here is a more profound solution to this problem. It enables you to create an entire VB6 template project usable for easy creation of console VB6 apps with both output and input. ("the brick" in the text in fact means "a VB6 building block")

This is a Google translation of the Russian original yet it seems quite understandable technically. At the bottom of the page there are also hints on how to create a copy of VB6 IDE that wouldn't crash when interactively debugging (pausing etc.) such apps that are totally "alien" to the windowed-only VB6.

John:
You're the man!

Thanks for putting some meaning into this project.

Mike Lobanovsky:
Glad you liked it. I've had such a setup for quite some time now matching my original VB6 and I've never had any problems with it, always creating console-mode VB6 samples for benchmark/comparison purposes if they don't explicitly need any GUI interaction.

John:
Attached is my best attempt to capture the content into a stable non-flashing PDF. I was unable to get the examples or template files. Not much here.  :'(


--- Code: Visual Basic ---Attribute VB_Name = "EasyConsole"' ******************************************************************************' *     Fire-Lines © 2013                                                      *' *     Ìîäóëü:                                                                *' *         EasyConsole                                                        *' *     Îïèñàíèå:                                                              *' *         Ýòîò ìîäóëü âíîñèò ðåâîëþöèîííî ïðîñòóþ ïîääåðæêó íàïèñàíèÿ êîí-   *' *         ñîëüíûõ ïðèëîæåíèé íà VB.                                          *' *     Àâòîð:                                                                 *' *         Âëàäèñëàâ Ïåòðîâñêèé (firehacker)                                  *' *     Èñòîðèÿ èçìåíåíèé:                                                     *' *         *   2013-01-19  firehacker  Ôàéë ñîçäàí.                           *' *                                                                            *' *     Òåêóùàÿ âåðñèÿ: 1.0.0                                                  *' *                                                                            *' ******************************************************************************Option Explicit Public stdin    As ITextStreamPublic stdout   As ITextStreamPublic stderr   As ITextStream Public Sub EasyconInitialize(Optional ByVal bUnicodeFormat As Boolean = False)    Dim fso As IFileSystem3        Set fso = New FileSystemObject        Set stdin = fso.GetStandardStream(0, bUnicodeFormat)    Set stdout = fso.GetStandardStream(1, bUnicodeFormat)    Set stderr = fso.GetStandardStream(2, bUnicodeFormat)End Sub Public Sub EasyconDeinitialize()    Set stdin = Nothing    Set stdout = Nothing    Set stderr = NothingEnd Sub 

Navigation

[0] Message Index

[#] Next page

Go to full version