Como ejecutar comandos de win en VB

Iniciado por peib0l, 22 Octubre 2008, 21:58 PM

0 Miembros y 1 Visitante están viendo este tema.

peib0l

bueno mi duda es esa como puedo hacer por ejemplo para darle a un boton y que por ejemplo haga un ping??

o cualqueir otro comando de win.. pero que me lo expliquen o me manden a un tutorial porque e mirado por google pero no encuentro lo que busco....

o tambien que escribas el comando y se te mueste el resultado en pantalla a traves de un textbox


Salu2 y gracias

& eDu &


aaronduran2

Y para ejecutar los comandos y mostrar el resultado en un TextBox:

Código (vb) [Seleccionar]
'Redirects output from console program to textbox.
'Requires two textboxes and one command button.
'Set MultiLine property of Text2 to true.
'
'Original bcx version of this program was made by
' dl <dl@tks.cjb.net>
'VB port was made by Jernej Simoncic <jernej@isg.si>
'Visit Jernejs site at http://www2.arnes.si/~sopjsimo/
'
'Note: don't run plain DOS programs with this example
'under Windows 95,98 and ME, as the program freezes when
'execution of program is finnished.
'Traducción de la forma de uso:
'Requiere un control Command Button y dos Text Box
'(el segundo con la propiedad Multiline en True)

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

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


Para usarlo:

Código (vb) [Seleccionar]
Redirect "comando", TextBox

Saludos.

peib0l

Cita de: ErMoja en 22 Octubre 2008, 22:04 PM
Código (vb) [Seleccionar]
Shell "cmd /c ping www.google.es"

:)

y como hago para que luego lo mueste en un textbox????


Salu2 y gracias de antemano

aaronduran2

Con lo que te he puesto arriba. Utiliza:

Código (vb) [Seleccionar]
Redirect "ping www.google.es", TextBox1.Text

Saludos.

& eDu &

Cita de: aaronduran2 en 23 Octubre 2008, 16:50 PM
Con lo que te he puesto arriba. Utiliza:

Código (vb) [Seleccionar]
Redirect "ping www.google.es", TextBox1.Text

Saludos.
TextBox1.Text esta mal, es solo TextBox1

:)

~~

No está mal ,se puede poner de las dos formas, ya que Text es la propiedad por defecto de n textBox, pero no está mal ponerla

& eDu &

Cita de: E0N en 23 Octubre 2008, 17:24 PM
No está mal ,se puede poner de las dos formas, ya que Text es la propiedad por defecto de n textBox, pero no está mal ponerla
Yo probé con Text1.Text y no me funcionaba en cambio le quité el . y el Text y me ruló bien.

aaronduran2

Cierto, no me había dado cuenta.

Saludos.

s E t H

no funciona el .text porque pide un objeto, no un string


ese codigo no ejecuta comandos internos como dir o cd, supongo que se soluciona usando "cmd.exe /c COMANDO", pero si el usuario ingresa el comando se complica, no?