aca t dejo el codigo:
1 form y 1 modulo
Controles:
-2 Command Buttons> cmdProcesos
cmdKill
-1 List View> Lvw
-1 ChekBox> chkPreguntar
Codigo en el form:
Private Sub cmdKill_Click()
If chkPreguntar.Value = 1 Then
If MsgBox("Esta seguro que desea terminar el proceso '" + Lvw.SelectedItem + "' ?", vbQuestion + vbYesNo) = vbYes Then
KillProcess (Lvw.SelectedItem)
cmdProcesos_Click
End If
Else
KillProcess (Lvw.SelectedItem)
cmdProcesos_Click
End If
End Sub
Private Sub cmdProcesos_Click()
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Lvw.ListItems.Clear
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0)
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc)
i = 0
While theloop <> 0
exename = proc.szExeFile
ret = Lvw.ListItems.Add(, "first" & CStr(i), exename)
Lvw.ListItems("first" & CStr(i)).SubItems(1) = proc.th32ProcessID
i = i + 1
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap
End Sub
Public Sub KillProcess(ByVal processName As String)
On Error GoTo ErrHandler
Dim oWMI
Dim ret
Dim sService
Dim oWMIServices
Dim oWMIService
Dim oServices
Dim oService
Dim servicename
Set oWMI = GetObject("winmgmts:")
Set oServices = oWMI.InstancesOf("win32_process")
For Each oService In oServices
servicename = LCase(Trim(CStr(oService.Name) & ""))
If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
ret = oService.Terminate
End If
Next
Set oServices = Nothing
Set oWMI = Nothing
ErrHandler:
Err.Clear
End Sub
Private Sub Form_Load()
Dim header As ColumnHeader
Lvw.View = lvwReport
Lvw.ColumnHeaders.Clear
Set header = Lvw.ColumnHeaders.Add(, "first", "Process", 2000)
Set header = Lvw.ColumnHeaders.Add(, "second", "ID", 950)
Lvw.Refresh
End Sub
Private Sub Form_Resize()
Lvw.Height = Me.Height - 500
Lvw.Width = Me.Width - 1575
cmdKill.Left = Lvw.Width + 100
cmdProcesos.Left = Lvw.Width + 100
End Sub
Codigo en el Modulo:
Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPheaplist = &H1
Public Const TH32CS_SNAPthread = &H4
Public Const TH32CS_SNAPmodule = &H8
Public Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Public Const MAX_PATH As Integer = 260
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
espero q te sirva
ak te dejo el codigo mas resumido, x si keres que termine un proceso concreto:
'--------------------------------------------------------------------
Private Sub Command1_Click()
KillProcess ("winword.exe") 'termina el proceso del Word
End Sub
Public Sub KillProcess(ByVal processName As String)
On Error GoTo ErrHandler
Dim oWMI
Dim ret
Dim sService
Dim oWMIServices
Dim oWMIService
Dim oServices
Dim oService
Dim servicename
Set oWMI = GetObject("winmgmts:")
Set oServices = oWMI.InstancesOf("win32_process")
For Each oService In oServices
servicename = LCase(Trim(CStr(oService.Name) & ""))
If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
ret = oService.Terminate
End If
Next
Set oServices = Nothing
Set oWMI = Nothing
ErrHandler:
Err.Clear
End Sub
'----------------------------------------------------------------------
PD: todavia no consigo que tire un msgbox cuando no lo puede cerrar
un abrazo