Newsgroup: it.comp.lang.visual-basic
Titolo: Catturare l'output di una shell dos

salve volevo sapere se qualcuno sa come fare. ho necessità di lanciare un
comando dos e visualizzarne il risultato nel form vb. Un esempio di questo è
www.burnatonce.com
, un programma di masterizzazione che usa un exe esterno a
shell e lo fa comparire all'interno del programma vb.
se qualcuno mi puo aiutare, ne avrei propio bisogno
grazie
fabrizio

comando dos e visualizzarne il risultato nel form vb. 
Prendi un form nuovo, chiamalo frmShelledApp e mettigli una textbox
multiline (magari con fondo nero e font di colore verde ;-)
<FORM CODE>
Option Explicit
Private Sub Form_Resize()
    txtOut.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
</FORM CODE>
Una funzione Main per lanciare il programma di esempio, che in questo
caso esegue una chiamata a netstat:
<MAIN CODE>
Public Sub main()
    frmShelledApp.Show
    frmShelledApp.Refresh
    DoEvents
    frmShelledApp.txtOut = ExecuteApp(Environ$("systemroot") &
"\system32\netstat.exe")
End Sub
</MAIN CODE>
Quindi in un modulo metti il codice che realizza il giochetto:
<MODULE CODE>
Option Explicit
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.
Private Function ExecuteApp(sCmdline As String) 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 will
be 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&)
        sOutput = sOutput & Left$(sBuffer, lngBytesRead)
    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
</MODULE CODE>
Premi Ctrl+F5 e gustati il risultato ;-)
-- 
ciao, hal

 ha scritto nel messaggio
news:mcjclvkg2a55vhr8t...@4ax.com...
