Newsgroup: it.comp.lang.visual-basic
Titolo: Catturare output processo esterno

Salve ragazzi,
Ho un problemino....il mio applicativo deve eseguire un programmino esterno
e catturare il suo output!
Premetto che il programmino esterno apre una finestra dos e stampa delle
righe...tipo un log...io devo prendere quelle righe in real-time ed
elaborarle!
Come posso catturare l'output???
Aspetto vostre risposte e intanto vi ringrazio
Alessio 

 ha scritto nel messaggio
news:Ztmne.4550$is5....@tornado.fastwebnet.it...

ciao!
estratto dalla mia tesi(che devo discutere da 2 anni circa...:( ):
dagli una pulita dove sono presenti Form2 e debugFrm e funziona alla grande
_____________________________________________________
Option Explicit
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal
hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal
lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long)
As Long
Private Const STARTF_USESTDHANDLES = &H100
Private Const STARTF_USESHOWWINDOW = &H1
Private Const SW_HIDE = 0
Private Const NORMAL_PRIORITY_CLASS = &H20
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 String
    lpDesktop As String
    lpTitle As String
    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 Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type
Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type
Private Type OVERLAPPED
    Internal As Long
    InternalHigh As Long
    offset As Long
    OffsetHigh As Long
    hEvent As Long
End Type
Private Declare Function CreatePipe Lib "kernel32" ( _
                            phReadPipe As Long, phWritePipe As Long, _
                            lpPipeAttributes As SECURITY_ATTRIBUTES, _
                            ByVal nSize As Long _
                            ) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA"
( _
                            ByVal lpApplicationName As String, _
                            ByVal lpCommandLine As String, _
                            ByRef lpProcessAttributes As
SECURITY_ATTRIBUTES, _
                            ByRef lpThreadAttributes As SECURITY_ATTRIBUTES,
_
                            ByVal bInheritHandles As Long, ByVal
dwCreationFlags As Long, _
                            ByRef lpEnvironment As Any, ByVal
lpCurrentDriectory As String, _
                            ByRef lpStartupInfo As STARTUPINFO, _
                            ByRef lpProcessInformation As
PROCESS_INFORMATION _
                            ) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByRef
lpBuffer As Any, _
                            ByVal nNumberOfBytesToRead As Long, _
                            ByRef lpNumberOfBytesRead As Long, _
                            ByRef lpOverlapped As Any _
                            ) As Long       ' ByRef lpOverlapped As
OVERLAPPED
'Runs an ms-dos application and returns
'text print to stdOutput and stdErr.
'This text would usualy be printed to the screen.
Public Function ExecuteApp(sCmdline As String, Optional skip As Boolean) As
String
    Dim proc As PROCESS_INFORMATION, ret As Long
    Dim start As STARTUPINFO
    Dim sa As SECURITY_ATTRIBUTES
    Dim hReadPipe As Long 'The handle used to read from the pipe.
    Dim hWritePipe As Long 'The pipe where StdOutput and StdErr willbe sent.
    Dim sOutput As String
    Dim lngBytesRead As Long, sBuffer As String * 256
    sa.nLength = Len(sa)
    sa.bInheritHandle = True
    ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
    If ret = 0 Then
        MsgBox "CreatePipe failed. Error: " & Err.LastDllError
        Exit Function
    End If
    start.cb = Len(start)
    start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
    ' Redirect the standard output and standard error to the same pipe
    start.hStdOutput = hWritePipe
    start.hStdError = hWritePipe
    start.wShowWindow = SW_HIDE
    ' Start the shelled application
    ret = CreateProcess(vbNullString, sCmdline, sa, sa, True,
NORMAL_PRIORITY_CLASS, _
                        ByVal vbNullString, ByVal vbNullString, start, proc)
    If ret = 0 Then
        MsgBox "CreateProcess failed. Error: " & Err.LastDllError
        Exit Function
    End If
    ' The handle wWritePipe has been inherited by the shelled application
    ' so we can close it now
    CloseHandle hWritePipe
    ' Read the characters that the shelled application
    ' has outputted 256 characters at a time
    Do
        ret = ReadFile(hReadPipe, ByVal sBuffer, 256, lngBytesRead, ByVal
0&)
        If skip = False Then
        If IsNumeric(Left$(sBuffer, lngBytesRead)) = False Then
            Form2.Label1 = Form2.Label1 & Left$(sBuffer, lngBytesRead)
        Else
            Form2.pb1.Value = CInt(Trim(Left$(Replace(sBuffer, ".", ","),
lngBytesRead)))
        End If
        debugfrm.Text1 = debugfrm.Text1 & Left$(sBuffer, lngBytesRead)
        sBuffer = ""
        Form2.Refresh
        End If
    Loop While ret <> 0 'if ret = 0 then there is no more characters to read
    CloseHandle proc.hProcess
    CloseHandle proc.hThread
    CloseHandle hReadPipe
    ExecuteApp = sOutput
End Function
_______________________________________
ciao
marco