• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 705
  • Last Modified:

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

0
thydzik
Asked:
thydzik
  • 3
  • 3
2 Solutions
 
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
thydzikAuthor 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
 
thydzikAuthor 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
 
thydzikAuthor Commented:
thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now