Results 1 to 7 of 7

Thread: redirect console output - vb6

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    2

    Unhappy redirect console output - vb6

    hi
    i want to invoke a fortran console application from vb and redirect its output to a textbox at real time.

    i tried the following code:

    Option Explicit
    Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
    Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
    Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

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

    Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
    End Type

    Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
    End Type

    Private Type STARTUPINFO
    cb As Long
    lpReserved As Long
    lpDesktop As Long
    lpTitle As Long
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
    End Type

    Private Type OVERLAPPED
    ternal As Long
    ternalHigh As Long
    offset As Long
    OffsetHigh As Long
    hEvent As Long
    End Type

    Private Const STARTF_USESHOWWINDOW = &H1
    Private Const STARTF_USESTDHANDLES = &H100
    Private Const SW_HIDE = 0
    Private Const EM_SETSEL = &HB1
    Private Const EM_REPLACESEL = &HC2

    Private Sub Command1_Click()
    Command1.Enabled = False
    Redirect Text1.Text, Text2

    Command1.Enabled = True
    End Sub

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If Command1.Enabled = False Then Cancel = True
    End Sub

    Sub Redirect(CmdLine As String, objTarget As Object)
    Dim i%, t$
    Dim pa As SECURITY_ATTRIBUTES
    Dim pra As SECURITY_ATTRIBUTES
    Dim tra As SECURITY_ATTRIBUTES
    Dim pi As PROCESS_INFORMATION
    Dim sui As STARTUPINFO
    Dim hRead As Long
    Dim hWrite As Long

    Dim bRead As Long
    Dim lpBuffer(1024) As Byte
    pa.nLength = Len(pa)
    pa.lpSecurityDescriptor = 0
    pa.bInheritHandle = True

    pra.nLength = Len(pra)
    tra.nLength = Len(tra)

    If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
    sui.cb = Len(sui)
    GetStartupInfo sui

    sui.hStdOutput = hWrite
    sui.hStdError = hWrite
    sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
    sui.wShowWindow = SW_HIDE
    If CreateProcess(vbNullString, CmdLine, pra, tra, True, 0, 0, vbNullString, sui, pi) <> 0 Then

    SetWindowText objTarget.hwnd, ""


    Do
    Erase lpBuffer()

    If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
    SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
    SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
    DoEvents
    Else
    CloseHandle pi.hThread
    CloseHandle pi.hProcess
    Exit Do
    End If
    CloseHandle hWrite
    Loop
    CloseHandle hRead
    End If
    End If

    End Sub

    it works fine with ping command...but gives the following error with the fortran application:

    forrtl: The handle is invalid.forrtl: severe (39): error during read, unit 5, file CONIN$

    i tried it with batch files by redirecting output to a file..this works...but iam able to display d output in d textbox only after the fortran application completes as i am unable to access the output file while it is being written...

    can sum1 plz offer a solution by either of d ways???its urgent n me all bugged up trying this ...thanx a lot....

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: redirect console output - vb6

    Please indent your code and use code tags to post it. It help to make it readable to other you want to enlist to help. Why not pipe the output directly to the textbox. I believe all you need is the textbox handle.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    2

    Re: redirect console output - vb6

    oops...sorry should hav made the code more readable....here is the code:

    Code:
    Option Explicit
    Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
    Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
    Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
    
    Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
    End Type
    
    Private Type STARTUPINFO
        cb As Long
        lpReserved As Long
        lpDesktop As Long
        lpTitle As Long
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Byte
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
        End Type
        
        Private Type OVERLAPPED
        ternal As Long
        ternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
    End Type
    
    Private Const STARTF_USESHOWWINDOW = &H1
    Private Const STARTF_USESTDHANDLES = &H100
    Private Const SW_HIDE = 0
    Private Const EM_SETSEL = &HB1
    Private Const EM_REPLACESEL = &HC2
    
    Private Sub Command1_Click()
        Command1.Enabled = False
        Redirect Text1.Text, Text2
        Command1.Enabled = True
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        If Command1.Enabled = False Then Cancel = True
    End Sub
    
    Sub Redirect(CmdLine As String, objTarget As Object)
        Dim i%, t$
        Dim pa As SECURITY_ATTRIBUTES
        Dim pra As SECURITY_ATTRIBUTES
        Dim tra As SECURITY_ATTRIBUTES
        Dim pi As PROCESS_INFORMATION
        Dim sui As STARTUPINFO
        Dim hRead As Long
        Dim hWrite As Long
        
        Dim bRead As Long
        Dim lpBuffer(1024) As Byte
        pa.nLength = Len(pa)
        pa.lpSecurityDescriptor = 0
        pa.bInheritHandle = True
        
        pra.nLength = Len(pra)
        tra.nLength = Len(tra)
    
        If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
            sui.cb = Len(sui)
            GetStartupInfo sui
            
            sui.hStdOutput = hWrite
            sui.hStdError = hWrite
            sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
            sui.wShowWindow = SW_HIDE
            If CreateProcess(vbNullString, CmdLine, pra, tra, True, 0, 0, vbNullString, sui, pi) <> 0 Then
    
                    SetWindowText objTarget.hwnd, ""
    
                    Do
                        Erase lpBuffer()
    
                        If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
                            SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
                            SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
                            DoEvents
                        Else
                            CloseHandle pi.hThread
                            CloseHandle pi.hProcess
                            Exit Do
                        End If
                        CloseHandle hWrite
                    Loop
                    CloseHandle hRead
            End If
        End If
    
    End Sub
    @ randem...
    I didt get wat u meant...as i m new to VB...so could u plzz tell me wat changes r required in code....???????

    Thnx in advance...

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: redirect console output - vb6

    Ok, I am looking at your code and Where does Text2 become an object? and what object does it become?

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: redirect console output - vb6

    This
    one worked for me

    Dana
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: redirect console output - vb6

    OK, I found it. You are using the example from All-API. It would help if you did not remove their comments... It would suggest you are attempting to pass their work off as your own.

    I will look into other programs piping data to the textbox.

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: redirect console output - vb6

    What command line are you using to execute the Fortran program thru the textbox?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width