How to build a Console Application using VB6

Topics on common programming languages
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to build a Console Application using VB6

Post by Neo » Wed Feb 03, 2010 3:29 am

One guy asked me about this and I thought of adding this to ROBOT.LK.

There are 4 important things to consider.
  1. Command$() and Environ$()
    First two simply VB commands. Command$ is used to read command line arguments and Environ$ is used to read environment variables.

  2. Standard I/O
    Add Microsoft Scripting Runtime from References. See following example for usage.

    Code: Select all

    'Requires a reference to Microsoft Scripting Runtime.
    Sub Main()
       Dim FSO As New Scripting.FileSystemObject
       Dim sin As Scripting.TextStream
       Dim sout As Scripting.TextStream
       Dim strWord As String
       
       Set sin = FSO.GetStandardStream(StdIn)
       Set sout = FSO.GetStandardStream(StdOut)
       sout.WriteLine "Hello!"
       sout.WriteLine "What's the word?"
       strWord = sin.ReadLine()
       sout.WriteLine "So, the word is " & strWord
       Set sout = Nothing
       Set sin = Nothing
    End Sub
    
  3. Return codes
    To exit and pass a return code you need another API call:

    Code: Select all

    Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
  4. Link as Console App
    Use following command to Re-Link the VB EXE as a Console Application. LINK.EXE comes with VB6.

    Code: Select all

    LINK.EXE /EDIT /SUBSYSTEM:CONSOLE YourVBEXEName.EXE
Post Reply

Return to “.Net & Other Programming”