I want to kill an application. For example internet explorer. However the windows caption is always different so i would like to kill it via it's filename (i.e iexplore.exe).
Help appreciated thanks :D
Printable View
I want to kill an application. For example internet explorer. However the windows caption is always different so i would like to kill it via it's filename (i.e iexplore.exe).
Help appreciated thanks :D
VB Code:
Option Explicit Option Compare Text 'IMPORTANT (assuming you do not want the exe name to be case sensitive) Private Declare Function OpenProcess Lib "KERNEL32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function TerminateProcess Lib "KERNEL32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function GetExitCodeProcess Lib "KERNEL32.dll" (ByVal hProcess As Long, ByRef lpExitCode As Long) As Long Private Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, ByRef lphModule As Any, ByVal cb As Long, ByRef cbNeeded As Long) As Long Private Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long Private Const PROCESS_VM_READ = 16 Private Const PROCESS_TERMINATE As Long = (&H1) Private Const PROCESS_QUERY_INFORMATION As Long = (&H400) Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Private Declare Function Process32First Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long Private Const TH32CS_SNAPPROCESS = &H2 Private Const MAX_PATH As Long = 260 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 * MAX_PATH End Type '******************************** 'Created by Mark Gordon (msg555) 'Date December 4th, 2004 ' 'Returns if the Function was succesfull ' EXEName - Name of the EXE of the process. It could also be a dll or other file '******************************** Public Function TerminateEXE(ByVal EXEName As String) As Boolean Dim hSnap As Long hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) If hSnap Then Dim Proc32 As PROCESSENTRY32, BackSlash As Long, Zero As Long, hName As String Proc32.dwSize = Len(Proc32) If Process32First(hSnap, Proc32) Then Do 'Do Some Parsing Zero = InStr(Proc32.szExeFile, Chr(0)) If Zero Then hName = Left(Proc32.szExeFile, Zero - 1) Else hName = Proc32.szExeFile End If BackSlash = InStrRev(hName, "\") If BackSlash Then hName = Mid(hName, BackSlash + 1) If hName = EXEName Then 'We have a match. TERMINATE Dim hProc As Long, hExitCode As Long hProc = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, 1, Proc32.th32ProcessID) GetExitCodeProcess hProc, hExitCode If TerminateProcess(hProc, hExitCode) Then TerminateEXE = True CloseHandle hProc CloseHandle hSnap Exit Function End If Loop While Process32Next(hSnap, Proc32) End If End If CloseHandle hSnap End Function
You can use Wokawidgit's code to terminate a runnng process.
Hey Deepak, that code wont work for a Windows 98 system. Terminating a process is
dependent on the OS. ;)
eh.. I don't have Win 98 System. K then BefunMunkToloGen u should try Woka's code.
Rob can u help me here