Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Console application query

Status
Not open for further replies.

j252ewv

Programmer
Jul 2, 2008
43
GB
Does anyone have a 'Hello World' application to write to a console window?
 
Is this question about hijacking a console window, simply writing to StdOut, or writing to the Console Device?

A VB6 program normally doesn't have a console window. Without tweaking tweaking they run in the Windows Subsystem anyway so it won't write to a console it was started from (i.e. by running via a command prompt).


If you're willing to mark the EXE for the Console Subsystem you can just use the StdOut stream from the FSO:
Code:
Option Explicit
'Requires a reference to Microsoft Scripting Runtime.

Private Sub Main()
    Dim stmStdOut As Scripting.TextStream
    Dim stmStdIn As Scripting.TextStream
    
    With New Scripting.FileSystemObject
        Set stmStdOut = .GetStandardStream(StdOut)
        Set stmStdIn = .GetStandardStream(StdIn)
    End With
    
    stmStdOut.WriteLine "Hello World"
    stmStdOut.Write "<enter to continue>"
    stmStdIn.ReadLine
End Sub
I generally relink the compiled EXE by dragging its icon in Windows Explorer onto the icon of this script I wrote:
Code:
Option Explicit
'LinkConsole.vbs
'
'This is a WSH script used to make it easier to edit a
'compiled VB6 EXE using LINK.EXE to create a console
'mode program.
'
'Drag the EXE's icon onto the icon for this file, or
'execute it from a command prompt as in:
'
' LinkConsole.vbs <EXEpath&file>
'
'Be sure to set up the LINK path to match your VB6 installation.

If WScript.Arguments.Count < 1 Then
  WScript.Echo "Missing EXE parameter." & vbNewLine _
             & "Use LinkConsole.vbs <EXEpath&file>" & vbNewLine _
             & "or drag EXE onto this script's icon."
Else
  With CreateObject("WScript.Shell")
    .Run """C:\Program Files\Microsoft Visual Studio\VB98\LINK.EXE""" _
       & " /EDIT /SUBSYSTEM:CONSOLE " _
       & """" & WScript.Arguments(0) & """"
  End With
  WScript.Echo "Complete!"
End If
With a little more work you can test it from inside the IDE by using API calls to allocate and then deallocate a console window. You can also replace the TextStreams by API calls to do the I/O as well.
 
There are also VB6 IDE add-ins that offer the option to hook the linking process and add the necessary parameters when you do a Make from within the IDE.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top