Matar un proceso
Hola, Me gustarÃa saber como se puede matar un proceso (aplicación externa) desde Visual Basic 6.0
Algo asà como el KILL -9 <num_proc> de UNIX
Algo asà como el KILL -9 <num_proc> de UNIX
Option Explicit
Public Function KillApp(myName As String) As Boolean
Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error GoTo Finish
appCount = 0
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
Else
List1.AddItem szExename
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
If KillApp Then MsgBox "Proceso eliminado con éxito!", vbInformation
Finish:
End Function
Private Sub Form_Load()
KillApp ("none")
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set frmMain = Nothing
End Sub
Private Sub mnuArchivo_Eliminar_Click()
Call tbrMain_ButtonClick(tbrMain.Buttons("cmdEliminar"))
End Sub
Private Sub mnuArchivo_Salir_Click()
Unload Me
End Sub
Private Sub mnuAyuda_AcercaDe_Click()
frmAcerca.Show vbModal
End Sub
Private Sub tbrMain_ButtonClick(ByVal Button As MSComctlLib.Button)
Dim Msg As String
If Button.Key = "cmdEliminar" Then
If List1.Text <> "Procesos" Then
Call KillApp(List1.Text)
End If
Else
Unload Me
End If
End Sub
Private Sub Timer1_Timer()
KillApp ("none")
End Sub
Public Function KillApp(myName As String) As Boolean
Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error GoTo Finish
appCount = 0
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
Else
List1.AddItem szExename
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
If KillApp Then MsgBox "Proceso eliminado con éxito!", vbInformation
Finish:
End Function
Private Sub Form_Load()
KillApp ("none")
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set frmMain = Nothing
End Sub
Private Sub mnuArchivo_Eliminar_Click()
Call tbrMain_ButtonClick(tbrMain.Buttons("cmdEliminar"))
End Sub
Private Sub mnuArchivo_Salir_Click()
Unload Me
End Sub
Private Sub mnuAyuda_AcercaDe_Click()
frmAcerca.Show vbModal
End Sub
Private Sub tbrMain_ButtonClick(ByVal Button As MSComctlLib.Button)
Dim Msg As String
If Button.Key = "cmdEliminar" Then
If List1.Text <> "Procesos" Then
Call KillApp(List1.Text)
End If
Else
Unload Me
End If
End Sub
Private Sub Timer1_Timer()
KillApp ("none")
End Sub
