Link to home
Start Free TrialLog in
Avatar of Travis Hydzik
Travis HydzikFlag for Australia

asked on

creating a VB6 console application capable of piping the output

I have successfully created a console application that outputs text to the console, see the code. However, I can't pipe this to a text file. ie project1.exe > c:\a.txt just creates and empty file.

comments on how to achieve this would be appreciated.

Note; I am also linking the application with link.exe

Option Explicit
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 WriteConsole _
    Lib "kernel32" Alias "WriteConsoleA" ( _
    ByVal hConsoleOutput As Long, _
    ByVal lpBuffer As Any, _
    ByVal nNumberOfCharsToWrite As Long, _
    ByVal lpNumberOfCharsWritten As Long, _
    ByVal lpReserved As Any) _
    As Long

Private Const STD_OUTPUT_HANDLE = -11&



Private Sub Main()
    Dim hConsoleOut As Long
    Dim MsgOut As String
    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
    MsgOut = "this is a test string"
    WriteConsole hConsoleOut, MsgOut, Len(MsgOut), vbNull, ByVal 0&
    FreeConsole
End Sub

Open in new window

Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

WriteConsole
http://msdn.microsoft.com/en-us/library/ms687401(VS.85).aspx 
The Remarks section has the follow information:
Both WriteConsole and WriteFile can be used for console I/O. While WriteConsole supports writing Unicode characters to a console screen buffer, WriteFile does not. However, WriteConsole fails if it is used with a standard handle that is redirected to a file.
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
Avatar of Travis Hydzik

ASKER

egl1044,

thanks for that, the only problem with the code is that
AttachConsole(ATTACH_PARENT_PROCESS) is false and it skips over this section
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
egl1044,

ahh, thanks, exactly the answer I was wanting.

I commented it out and it worked well, so was wondering why it was included
thank you.