unit KillProcess;
interface
uses
Windows, SysUtils, Forms, Controls, StdCtrls, Classes, TlHelp32;
function KillByProcessNameOrModuleName(ProcessNameOrModuleName: string):Boolean;
function KillByProcessName(ProcessName: string):Boolean;
implementation
function KillByProcessNameOrModuleName(ProcessNameOrModuleName: string):Boolean;
function ModuleEnum(processid:Dword):boolean;
var
ModuleList :Thandle;
pm :TMODULEENTRY32;
foundModule:boolean;
begin
Result:=False;
ModuleList := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,processID);
pm.dwSize:=sizeof(TMODULEENTRY32);
foundModule:=module32first(ModuleList,pm);
while integer(foundModule)<>0 do begin
if pos(UpperCase(pm.szModule),UpperCase(ProcessNameOrModuleName))>0 then begin
Result:=True;
exit;
end;
foundModule:=Module32Next(ModuleList,pm);
end;
closehandle(ModuleList);
end;
var
Found: boolean;
HProcess,HSnapshot: THandle;
appe: TProcessEntry32;
begin
Result := False;
HSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
appe.dwSize := SizeOf(appe);
Found := Process32First(HSnapshot, appe);
while Integer(Found) <> 0do begin
if ModuleEnum(appe.th32ProcessID) then begin
try
HProcess := OpenProcess(PROCESS_ALL_ACCESS, True, appe.th32ProcessID);
TerminateProcess(HProcess, 0);
Result := True;
except
Result := False;
end;
end;
Found := Process32Next(HSnapshot, appe);
end;
CloseHandle(HSnapshot);
end;
function KillByProcessName(ProcessName: string):Boolean;
var
s: string;
Found: boolean;
HProcess,HSnapshot: THandle;
appe: TProcessEntry32;
begin
Result := False;
HSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
appe.dwSize := SizeOf(appe);
Found := Process32First(HSnapshot, appe);
while Integer(Found) <> 0do begin
s := appe.szExeFile;
if UpperCase(s) = UpperCase(ProcessName) then begin
try
HProcess := OpenProcess(PROCESS_ALL_ACCESS, True, appe.th32ProcessID);
TerminateProcess(HProcess, 0);
Result := True;
except
Result := False;
end;
end;
Found := Process32Next(HSnapshot, appe);
end;
CloseHandle(HSnapshot);
end;
end.