Cómo matar un proceso si se conoce el nombre del archivo EXE. Un método más seguro para cerrar aplicaciones en vez de utilizar el título de su ventana principal.
Este ejemplo muestra cómo cerrar un proceso con la API de Windows, y utilizando el nombre del EXE a cerrar. Código: Crear un proyecto nuevo de Visual Basic y en el Form1 colocar un TextBox y CommandButton, luego pegar el siguiente código.
Option Explicit Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" _ (ByVal lFlags As Long, ByVal lProcessID As Long) As Long Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" _ (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" _ (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long) Private 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 * 260 End Type Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _ ByVal uExitCode As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long Private Const PROCESS_TERMINATE = &H1; Private Const PROCESS_CREATE_THREAD = &H2; Private Const PROCESS_VM_OPERATION = &H8; Private Const PROCESS_VM_READ = &H10; Private Const PROCESS_VM_WRITE = &H20; Private Const PROCESS_DUP_HANDLE = &H40; Private Const PROCESS_CREATE_PROCESS = &H80; Private Const PROCESS_SET_QUOTA = &H100; Private Const PROCESS_SET_INFORMATION = &H200; Private Const PROCESS_QUERY_INFORMATION = &H400; Private Const STANDARD_RIGHTS_REQUIRED = &HF0000; Private Const SYNCHRONIZE = &H100000; Private Const PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF; Private Sub Command1_Click() Dim hSnapShot As Long, uProceso As PROCESSENTRY32 Dim res As Long, dwIDProceso As Long Dim hProceso As Long, Resultado As Long dwIDProceso = -1 hSnapShot = CreateToolhelpSnapshot(2&, 0&) If hSnapShot <> 0 Then uProceso.dwSize = Len(uProceso) res = ProcessFirst(hSnapShot, uProceso) Do While res If Left$(uProceso.szExeFile, InStr(uProceso.szExeFile, Chr$(0)) - 1) = LCase(Text1.Text) Then 'Si se encuentra el proceso, se guarda su ID. dwIDProceso = uProceso.th32ProcessID Exit Do End If res = ProcessNext(hSnapShot, uProceso) Loop Call CloseHandle(hSnapShot) End If 'Si el proceso no está abierto... If dwIDProceso = -1 Then MsgBox "El proceso """ & LCase(Text1.Text) & """ no está abierto" Else hProceso = OpenProcess(PROCESS_TERMINATE, True, dwIDProceso) Resultado = TerminateProcess(hProceso, 99) CloseHandle hProceso 'Si se cerró correctamente... If Resultado <> 0 Then MsgBox "El proceso """ & LCase(Text1.Text) & """ ha sido cerrado correctamente." Else MsgBox "El proceso """ & LCase(Text1.Text) & """ no pudo ser cerrado." End If End If End Sub
Ejecutar la aplicación y escribir en el cuadro de texto el nombre del EXE a cerrar y luego pulsar el botón.