elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  [SOLUCIONADO] CMD Pipe en Windows 7
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [SOLUCIONADO] CMD Pipe en Windows 7  (Leído 2,332 veces)
aaronduran2


Desconectado Desconectado

Mensajes: 790



Ver Perfil WWW
[SOLUCIONADO] CMD Pipe en Windows 7
« en: 4 Octubre 2010, 16:20 pm »

Hola. Quería implementar un código para hacer un pipe del CMD en una pequeña aplicación, pero no funciona en Windows 7.

El código es este:

Código
  1. 'Redirects output from console program to textbox.
  2. '
  3. 'Original bcx version of this program was made by
  4. ' dl <dl@tks.cjb.net>
  5. 'VB port was made by Jernej Simoncic <jernej@isg.si>
  6. 'Visit Jernejs site at [url]http://www2.arnes.si/~sopjsimo/[/url]
  7. '
  8. 'Note: don't run plain DOS programs with this example
  9. 'under Windows 95,98 and ME, as the program freezes when
  10. 'execution of program is finnished.
  11.  
  12. Option Explicit
  13. Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
  14. Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
  15. 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
  16. Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
  17. 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
  18. 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
  19. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  20.  
  21. Private Type SECURITY_ATTRIBUTES
  22. nLength As Long
  23. lpSecurityDescriptor As Long
  24. bInheritHandle As Long
  25. End Type
  26.  
  27. Private Type PROCESS_INFORMATION
  28. hProcess As Long
  29. hThread As Long
  30. dwProcessId As Long
  31. dwThreadId As Long
  32. End Type
  33.  
  34. Private Type STARTUPINFO
  35. cb As Long
  36. lpReserved As Long
  37. lpDesktop As Long
  38. lpTitle As Long
  39. dwX As Long
  40. dwY As Long
  41. dwXSize As Long
  42. dwYSize As Long
  43. dwXCountChars As Long
  44. dwYCountChars As Long
  45. dwFillAttribute As Long
  46. dwFlags As Long
  47. wShowWindow As Integer
  48. cbReserved2 As Integer
  49. lpReserved2 As Byte
  50. hStdInput As Long
  51. hStdOutput As Long
  52. hStdError As Long
  53. End Type
  54.  
  55. Private Type OVERLAPPED
  56. ternal As Long
  57. ternalHigh As Long
  58. offset As Long
  59. OffsetHigh As Long
  60. hEvent As Long
  61. End Type
  62.  
  63. Private Const STARTF_USESHOWWINDOW = &H1
  64. Private Const STARTF_USESTDHANDLES = &H100
  65. Private Const SW_HIDE = 0
  66. Private Const EM_SETSEL = &HB1
  67. Private Const EM_REPLACESEL = &HC2
  68.  
  69.  
  70. Sub Redirect(cmdLine As String, objTarget As Object)
  71. Dim i%, t$
  72. Dim pa As SECURITY_ATTRIBUTES
  73. Dim pra As SECURITY_ATTRIBUTES
  74. Dim tra As SECURITY_ATTRIBUTES
  75. Dim pi As PROCESS_INFORMATION
  76. Dim sui As STARTUPINFO
  77. Dim hRead As Long
  78. Dim hWrite As Long
  79. Dim bRead As Long
  80. Dim lpBuffer(1024) As Byte
  81. pa.nLength = Len(pa)
  82. pa.lpSecurityDescriptor = 0
  83. pa.bInheritHandle = True
  84.  
  85. pra.nLength = Len(pra)
  86. tra.nLength = Len(tra)
  87.  
  88. If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
  89.    sui.cb = Len(sui)
  90.    GetStartupInfo sui
  91.    sui.hStdOutput = hWrite
  92.    sui.hStdError = hWrite
  93.    sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
  94.    sui.wShowWindow = SW_HIDE
  95.    If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then
  96.        SetWindowText objTarget.hwnd, ""
  97.        Do
  98.            Erase lpBuffer()
  99.            If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
  100.                SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
  101.                SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
  102.                DoEvents
  103.            Else
  104.                CloseHandle pi.hThread
  105.                CloseHandle pi.hProcess
  106.                Exit Do
  107.            End If
  108.            CloseHandle hWrite
  109.        Loop
  110.        CloseHandle hRead
  111.    End If
  112. End If
  113. End Sub

¿Se podría adaptar a Windows 7?

Un saludo.


« Última modificación: 4 Octubre 2010, 16:26 pm por aaronduran2 » En línea

aaronduran2


Desconectado Desconectado

Mensajes: 790



Ver Perfil WWW
Re: CMD Pipe en Windows 7
« Respuesta #1 en: 4 Octubre 2010, 16:26 pm »

Bueno, buscando un poco más a fondo encontré un código que funciona perfectamente:

Código
  1. Option Explicit
  2.  
  3. Private Declare Function CreatePipe Lib "kernel32.dll" (ByRef phReadPipe As Long, ByRef phWritePipe As Long, ByRef lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
  4. Private Declare Function CreateProcess Lib "kernel32.dll" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByRef lpProcessAttributes As Long, ByRef lpThreadAttributes As Long, 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
  5. Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
  6. Private Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long, ByRef lpOverlapped As Long) As Long
  7.  
  8. Private Const STARTF_USESHOWWINDOW As Long = &H1
  9. Private Const STARTF_USESTDHANDLES As Long = &H100
  10. Private Const SW_HIDE As Long = 0
  11.  
  12. Private Type SECURITY_ATTRIBUTES
  13.    nLength As Long
  14.    lpSecurityDescriptor As Long
  15.    bInheritHandle As Long
  16. End Type
  17.  
  18. Private Type STARTUPINFO
  19.    cb As Long
  20.    lpReserved As Long
  21.    lpDesktop As Long
  22.    lpTitle As Long
  23.    dwX As Long
  24.    dwY As Long
  25.    dwXSize As Long
  26.    dwYSize As Long
  27.    dwXCountChars As Long
  28.    dwYCountChars As Long
  29.    dwFillAttribute As Long
  30.    dwFlags As Long
  31.    wShowWindow As Integer
  32.    cbReserved2 As Integer
  33.    lpReserved2 As Byte
  34.    hStdInput As Long
  35.    hStdOutput As Long
  36.    hStdError As Long
  37. End Type
  38.  
  39. Private Type PROCESS_INFORMATION
  40.    hProcess As Long
  41.    hThread As Long
  42.    dwProcessId As Long
  43.    dwThreadId As Long
  44. End Type
  45.  
  46. Public Sub ExecAndCapture(ByVal sCommandLine As String, cTextBox As TextBox, Optional ByVal sStartInFolder As String = vbNullString)
  47.  
  48. Const BUFSIZE As Long = 1024 * 10
  49. Dim hPipeRead As Long
  50. Dim hPipeWrite As Long
  51. Dim sa As SECURITY_ATTRIBUTES
  52. Dim si As STARTUPINFO
  53. Dim pi As PROCESS_INFORMATION
  54. Dim baOutput(BUFSIZE) As Byte
  55. Dim sOutput As String
  56. Dim lBytesRead As Long
  57.  
  58.    With sa
  59.        .nLength = Len(sa)
  60.        .bInheritHandle = 1    ' get inheritable pipe handles
  61.    End With
  62.  
  63.    If CreatePipe(hPipeRead, hPipeWrite, sa, 0) = 0 Then
  64.        Exit Sub
  65.    End If
  66.  
  67.    With si
  68.        .cb = Len(si)
  69.        .dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
  70.        .wShowWindow = SW_HIDE          ' hide the window
  71.        .hStdOutput = hPipeWrite
  72.        .hStdError = hPipeWrite
  73.    End With
  74.  
  75.    If CreateProcess(vbNullString, sCommandLine, ByVal 0&, ByVal 0&, 1, 0&, ByVal 0&, sStartInFolder, si, pi) Then
  76.        Call CloseHandle(hPipeWrite)
  77.        Call CloseHandle(pi.hThread)
  78.        hPipeWrite = 0
  79.        Do
  80.            DoEvents
  81.            If ReadFile(hPipeRead, baOutput(0), BUFSIZE, lBytesRead, ByVal 0&) = 0 Then
  82.                Exit Do
  83.            End If
  84.            sOutput = Left$(StrConv(baOutput(), vbUnicode), lBytesRead)
  85.            cTextBox.SelText = sOutput
  86.        Loop
  87.        Call CloseHandle(pi.hProcess)
  88.    End If
  89.    Call CloseHandle(hPipeRead)
  90.    Call CloseHandle(hPipeWrite)
  91.  
  92. End Sub

Se puede cerrar el tema.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Pasar más de un int con una pipe
Programación C/C++
ithory 2 5,779 Último mensaje 15 Diciembre 2012, 13:23 pm
por ithory
pipe y QT!
Programación C/C++
febef 2 2,145 Último mensaje 17 Abril 2013, 01:26 am
por febef
Pipe doble
GNU/Linux
desikoder 3 3,238 Último mensaje 12 Noviembre 2013, 17:07 pm
por desikoder
Windows 8.1 Ayuda - SOLUCIONADO « 1 2 »
Windows
Zorronde 15 9,375 Último mensaje 26 Septiembre 2014, 03:27 am
por Zorronde
Consola por pipe crashea en windows 8 y versiones adelantadas
Programación Visual Basic
illuminat3d 9 4,105 Último mensaje 7 Abril 2016, 19:31 pm
por Lekim
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines