how to redirect standard i/o of a console app to a text file

Using VB6. Already done console portion, but would like to dump contents of console to a text file.

Thanks.


Comments



  • Got the sample from Microsoft article ID# Q171654 ( How to attach a console window to your visual basic program). Works great, but I need to dump contents of the console to a text file.


    The program is:


    Option Explicit

    Dim hConsole As Long


    Private Sub Command1_Click()

    Dim Result As Long, sOut As String, cWritten As Long



    sOut = "Hello World, testing STDOUT" & vbCrLf

    Result = WriteConsole(hConsole, ByVal sOut, Len(sOut), _

    cWritten, ByVal 0&)



    Shell "C: emp est1.bat"

    'Shell "C:ORAWIN95BINR25RUN32.EXE CMDFILE=c:soe_emsVO_PLPR_UPDTE.cmd"







    End Sub


    Private Sub Command2_Click()



    Dim Result, FileResult As Long, sOut As String, cWritten As Long

    Dim TestStd



    sOut = "Hello World, testing STDOUT" & vbCrLf

    Result = WriteConsole(hConsole, ByVal sOut, Len(sOut), _

    cWritten, ByVal 0&)





    Shell "C:ORAWIN95BINPLUS33W.EXE ops$soe/[email protected]:kaley:SOE @c: emp est_sql.sql"





    End Sub


    Private Sub Form_Load()



    If AllocConsole() Then

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE)

    If hConsole = 0 Then

    MsgBox "Could not allocate STDOUT"

    End If

    Else

    MsgBox "Could not allocate console"

    End If







    End Sub


    Private Sub Form_Unload(Cancel As Integer)

    CloseHandle hConsole

    FreeConsole

    End Sub


    ############################################################


    The module.bas is:


    Option Explicit


    Declare Function AllocConsole Lib "kernel32" () As Long

    Declare Function FreeConsole Lib "kernel32" () As Long

    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

    Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long

    Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" _

    (ByVal hConsoleOutput As Long, lpBuffer As Any, ByVal _

    nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, _

    lpReserved As Any) As Long

    Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, _

    lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _

    lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long



    Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _

    lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _

    lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long



    Declare Function CreateFile Lib "kernel32" _

    Alias "CreateFileA" (ByVal lpFileName As String, _

    ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _

    ByVal lpSecurityAttributes As Long, _

    ByVal dwCreationDisposition As Long, _

    ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) _

    As Long

    Declare Function FlushFileBuffers Lib "kernel32" ( _

    ByVal hFile As Long) As Long




    Public Const GENERIC_WRITE = &H40000000

    Public Const GENERIC_READ = &H80000000

    Const FILE_ATTRIBUTE_NORMAL = &H80

    Const CREATE_ALWAYS = 2

    Const OPEN_ALWAYS = 4

    Const INVALID_HANDLE_VALUE = -1


    Const FILE_NAME = "test_sql.output"



    Public Const STD_OUTPUT_HANDLE = -11&


    ###########################################################


    Thanks for your help.





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories