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

LVL 11
TravisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nffvrxqgrcfqvvcCommented:
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.
0
nffvrxqgrcfqvvcCommented:
This example attatches to (cmd) and should work for file redirect.
Start Run > cmd
C:\>Project1 >C:\TEST.TXT

Option Explicit

Private Const INVALID_HANDLE_VALUE = (-1)
Private Const STD_OUTPUT_HANDLE = (-11)
Private Const ATTACH_PARENT_PROCESS = (-1)

Private Declare Function AttachConsole Lib "kernel32" (ByVal dwProcessId As Long) As Long
Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long

Dim hStdOut         As Long

Private Function ConsoleWrite( _
ByVal hStdOut As Long, _
ByVal data As String) As Boolean
    Dim Buffer()    As Byte
    Dim cb          As Long
    Buffer = StrConv(data, vbFromUnicode)
    ConsoleWrite = WriteFile(hStdOut, VarPtr(Buffer(0)), Len(data), cb, 0)
    Erase Buffer
End Function

Public Sub Main()
    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)
    If hStdOut = INVALID_HANDLE_VALUE Then
        Exit Sub ' _return
    End If
    If AttachConsole(ATTACH_PARENT_PROCESS) Then
        ConsoleWrite hStdOut, "This is a test"
        Call FreeConsole
    End If
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

TravisAuthor Commented:
egl1044,

thanks for that, the only problem with the code is that
AttachConsole(ATTACH_PARENT_PROCESS) is false and it skips over this section
0
nffvrxqgrcfqvvcCommented:
The example assumes you opened a command window (cmd) and type Project1 >C:\test.txt
If you already created a "real" console by changing the PE header then you can remove that line....
0
TravisAuthor Commented:
egl1044,

ahh, thanks, exactly the answer I was wanting.

I commented it out and it worked well, so was wondering why it was included
0
TravisAuthor Commented:
thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.