Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1

    [VB6] Problemi di DOS Output Redirect

    Ho bisogno dell'aiuto di qualcuno più esperto di me nell'uso del Visual Basic 6.

    Il codice sottostante permette di reindirizzare l'output di un programma DOS verso la TextBox "Text2"

    codice:
    'Redirects output from console program to textbox.
    'Requires two textboxes: "Text1" and "Text2"
    '(set MultiLine property of Text2 to true)
    'and one command button: "Command1"
    
    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_Load()
        
       Text1.Text = "cdrdao_scanbus.bat"
    
    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, Null, 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
    Io utilizzo il codice soprastante per lanciare tramite un file Batch il programma di masterizzazione CDRDAO e trasferirne l'Output Nella Text2.
    codice:
    Private Sub Form_Load()
    Text1.Text = "cdrdao_scanbus.bat"
    End Sub
    Il contenuto del file Batch è:
    cd cdrdao
    cdrdao scanbus

    Il problema è rappresentato dal fatto che l'incolonnamento dei dati nella Text2 differisce da quella della finestra DOS.
    Allego un'immagine per farvi capire meglio ciò che sto dicendo.

    Qualcuno di voi potrebbe dirmi come modificare il codice postato, onde ottenere nella TextBox una replica esatta della finestra DOS?

    Ringrazio anticipatamente tutti coloro che vorranno aiutarmi.
    Immagini allegate Immagini allegate

  2. #2
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Hai provato ad impostare il font del Textbox uguale a quello usato dalla finestra DOS?


  3. #3
    Ho provato a cambiare Font ma non ho risolto il problema.

    Grazie comunque per l'interessamento.

  4. #4
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Originariamente inviato da giancarlopell
    Ho provato a cambiare Font ma non ho risolto il problema.
    Strano, io invece sì.

    Originariamente inviato da giancarlopell
    Grazie comunque per l'interessamento.
    Prego, siamo qui per questo, no?


  5. #5
    Allora sono io che sbaglio qualcosa!
    Che font dovrei utilizzare?
    Lucida Console va bene?

    Grazie di nuovo.

  6. #6
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Un qualsiasi font mono-spaziato dovrebbe andar bene.
    Io ho usato il Courier New, ma anche il Lucida Console è mono-spaziato ed è usato di default con il Blocco Note.

    Non è per caso che la tua TextBox è troppo piccola?

    Se no, spiega bene il tuo problema, magari posta un'immagine che mostri il risultato che ottieni.


  7. #7
    Ho provato sia con Courier New che con Lucida Console ma il risultato non cambia.

    L'immagine che mostra la natura del problema l'ho già inviata all'inizio di questo thread:
    in alto la finestra DOS con la formattazione corretta e sotto il risultato che ottengo nella TextBox, utilizzando il codice postato.

    Grazie di nuovo.

  8. #8
    Utente di HTML.it L'avatar di gibra
    Registrato dal
    Apr 2008
    residenza
    Italy
    Messaggi
    4,244
    Originariamente inviato da giancarlopell
    L'immagine che mostra la natura del problema l'ho già inviata all'inizio di questo thread:
    in alto la finestra DOS con la formattazione corretta e sotto il risultato che ottengo nella TextBox, utilizzando il codice postato.
    Questo era PRIMA. ma il DOPO la modifica?
    Il font nel TextBox che mostri non mi sembra né Lucida né Courier...

  9. #9
    Infatti si tratta del classicissimo Times New Roman.

    Non ho ritenuto necessario inviare un'altra immagine perchè con qualunque font il risultato non cambia, rimane cioè perfettamente uguale a quello dell'esempio postato ad inizio Thread.

    Ciao di nuovo.

  10. #10

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.