Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: Fucko en 12 Abril 2021, 04:36 am



Título: Obtener retorno de consola en un tetxtbox o richtext. Ayuda
Publicado por: Fucko en 12 Abril 2021, 04:36 am
Hola, necesito leer el retorno de consola, en un textbox, un richtextbox
Encontré algo que me sirve, pero no para todas las aplicaciones de consola.
Por ej, comando de win, ping, route, etc va.
Pero si quiero leer el valor leido desde una consola fastboot (ya todos la conocen)

la pantalla queda negra.

adjunto code

Código:
Private Sub Command1_Click()
    Dim Exec As String
    Exec = ("C:\Windows\System32\route.exe" & " " & "ADD " & Text1 & " " & " MASK 255.255.255.255 192.168.1.1")
    txt_resultado.Text = ejecutar_Dos(Trim(Exec))
   

End Sub


Function ejecutar_Dos(Comando As String) As String
    Dim oShell As WshShell
    Dim oExec As WshExec
    Dim ret As String
     
    Set oShell = New WshShell
    DoEvents
     
    ' ejecutar el comando
    Set oExec = oShell.Exec("%comspec% /c " & Comando)
    ret = oExec.StdOut.ReadAll()
       
    ' retornar la salida y devolverla a la función
    ejecutar_Dos = ret ' Replace(ret, Chr(10), vbNewLine)
     
    DoEvents
    Me.SetFocus
End Function

el code de ejemplo va bien, con dir, ping, route

al poner por ejemplo, fastboot devices, el code arroja el dispositivo conectado, pero por ejemplo, al hacer fastboot getvar all, que me daría toda la info del movil, no arroja resultado...

alguna idea que puede ser?

algún code funcional?
gracias


Título: Re: Obtener retorno de consola en un tetxtbox o richtext. Ayuda
Publicado por: BlackZeroX en 29 Mayo 2021, 23:02 pm
Debes usar pipes.

El codigo si no te funciona te puede guiar, lo que hace es crear una instancia del shell y redirecciona la salida a una cadena ya despues puedes pasar esa salida a tu textbox.

Código
  1. Option Explicit
  2. Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MsgType, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
  3. Private Declare Function TranslateMessage Lib "user32" (ByRef lpMsg As Any) As Long
  4. Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageW" (ByRef lpMsg As Any) As Long
  5. Private Type POINTAPI
  6.    x As Long
  7.    Y As Long
  8. End Type
  9. Private Type MsgType
  10.    hWnd        As Long
  11.    message     As Long
  12.    wParam      As Long
  13.    lParam      As Long
  14.    Time        As Long
  15.    pt          As POINTAPI
  16. End Type
  17. Private Const PM_NOREMOVE           As Long = 0&
  18. Private Const PM_REMOVE             As Long = 1&
  19. '
  20. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  21. 'The CreatePipe function creates an anonymous pipe,
  22. 'and returns handles to the read and write ends of the pipe.
  23. Private Declare Function CreatePipe Lib "kernel32" ( _
  24.    phReadPipe As Long, _
  25.    phWritePipe As Long, _
  26.    lpPipeAttributes As Any, _
  27.    ByVal nSize As Long) As Long
  28.  
  29. 'Used to read the the pipe filled by the process create
  30. 'with the CretaProcessA function
  31. Private Declare Function ReadFile Lib "kernel32" ( _
  32.    ByVal hFile As Long, _
  33.    ByVal lpBuffer As String, _
  34.    ByVal nNumberOfBytesToRead As Long, _
  35.    lpNumberOfBytesRead As Long, _
  36.    ByVal lpOverlapped As Any) As Long
  37.  
  38. 'Structure used by the CreateProcessA function
  39. Private Type SECURITY_ATTRIBUTES
  40.    nLength As Long
  41.    lpSecurityDescriptor As Long
  42.    bInheritHandle As Long
  43. End Type
  44.  
  45. 'Structure used by the CreateProcessA function
  46. Private Type STARTUPINFO
  47.    cb As Long
  48.    lpReserved As Long
  49.    lpDesktop As Long
  50.    lpTitle As Long
  51.    dwX As Long
  52.    dwY As Long
  53.    dwXSize As Long
  54.    dwYSize As Long
  55.    dwXCountChars As Long
  56.    dwYCountChars As Long
  57.    dwFillAttribute As Long
  58.    dwFlags As Long
  59.    wShowWindow As Integer
  60.    cbReserved2 As Integer
  61.    lpReserved2 As Long
  62.    hStdInput As Long
  63.    hStdOutput As Long
  64.    hStdError As Long
  65. End Type
  66.  
  67. 'Structure used by the CreateProcessA function
  68. Private Type PROCESS_INFORMATION
  69.    hProcess As Long
  70.    hThread As Long
  71.    dwProcessID As Long
  72.    dwThreadID As Long
  73. End Type
  74.  
  75. 'This function launch the the commend and return the relative process
  76. 'into the PRECESS_INFORMATION structure
  77. Private Declare Function CreateProcessA Lib "kernel32" ( _
  78.    ByVal lpApplicationName As Long, _
  79.    ByVal lpCommandLine As String, _
  80.    lpProcessAttributes As SECURITY_ATTRIBUTES, _
  81.    lpThreadAttributes As SECURITY_ATTRIBUTES, _
  82.    ByVal bInheritHandles As Long, _
  83.    ByVal dwCreationFlags As Long, _
  84.    ByVal lpEnvironment As Long, _
  85.    ByVal lpCurrentDirectory As Long, _
  86.    lpStartupInfo As STARTUPINFO, _
  87.    lpProcessInformation As PROCESS_INFORMATION) As Long
  88.  
  89. 'Close opened handle
  90. Private Declare Function CloseHandle Lib "kernel32" ( _
  91.    ByVal hHandle As Long) As Long
  92.  
  93. 'Consts for the above functions
  94. Private Const NORMAL_PRIORITY_CLASS = &H20&
  95. Private Const STARTF_USESTDHANDLES = &H100&
  96. Private Const STARTF_USESHOWWINDOW = &H1
  97.  
  98.  
  99. Private mCommand As String          'Private variable for the CommandLine property
  100. Private mOutputs As String          'Private variable for the ReadOnly Outputs property
  101.  
  102. 'Event that notify the temporary buffer to the object
  103. Public Event ReceiveOutputs(CommandOutputs As String)
  104.  
  105. 'This property set and get the DOS command line
  106. 'It's possible to set this property directly from the
  107. 'parameter of the ExecuteCommand method
  108. Public Property Let CommandLine(DOSCommand As String)
  109.    mCommand = DOSCommand
  110. End Property
  111.  
  112. Public Property Get CommandLine() As String
  113.    CommandLine = mCommand
  114. End Property
  115.  
  116. 'This property ReadOnly get the complete output after
  117. 'a command execution
  118. Public Property Get Outputs()
  119.    Outputs = mOutputs
  120. End Property
  121.  
  122. Public Function ExecuteCommand(Optional CommandLine As String) As String
  123.    Dim proc As PROCESS_INFORMATION     'Process info filled by CreateProcessA
  124.    Dim ret As Long                     'long variable for get the return value of the
  125.                                        'API functions
  126.    Dim start As STARTUPINFO            'StartUp Info passed to the CreateProceeeA
  127.                                        'function
  128.    Dim sa As SECURITY_ATTRIBUTES       'Security Attributes passeed to the
  129.                                        'CreateProcessA function
  130.    Dim hReadPipe As Long               'Read Pipe handle created by CreatePipe
  131.    Dim hWritePipe As Long              'Write Pite handle created by CreatePipe
  132.    Dim lngBytesread As Long            'Amount of byte read from the Read Pipe handle
  133.    Dim strBuff As String * 256         'String buffer reading the Pipe
  134.  
  135.    'if the parameter is not empty update the CommandLine property
  136.    If Len(CommandLine) > 0 Then
  137.        mCommand = CommandLine
  138.    End If
  139.  
  140.    'if the command line is empty then exit whit a error message
  141.    If Len(mCommand) = 0 Then
  142.        MsgBox "Command Line empty", vbCritical
  143.        Exit Function
  144.    End If
  145.  
  146.    'Create the Pipe
  147.    sa.nLength = Len(sa)
  148.    sa.bInheritHandle = 1&
  149.    sa.lpSecurityDescriptor = 0&
  150.    ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
  151.  
  152.    If ret = 0 Then
  153.        'If an error occur during the Pipe creation exit
  154.        MsgBox "CreatePipe failed. Error: " & Err.LastDllError, vbCritical
  155.        Exit Function
  156.    End If
  157.  
  158.    'Launch the command line application
  159.    start.cb = Len(start)
  160.    start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
  161.    'set the StdOutput and the StdError output to the same Write Pipe handle
  162.    start.hStdOutput = hWritePipe
  163.    start.hStdError = hWritePipe
  164.    'Execute the command
  165.    ret& = CreateProcessA(0&, mCommand, sa, sa, 1&, _
  166.        NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
  167.  
  168.    If ret <> 1 Then
  169.        'if the command is not found ....
  170.        MsgBox "File or command not found", vbCritical
  171.        Exit Function
  172.    End If
  173.  
  174.    'No
  175.  

Dulces lunas!¡.