1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| function KillProcess(const ProcessName : string) : boolean;
var ProcessEntry32 : TProcessEntry32;
HSnapShot : THandle;
HProcess : THandle;
begin
Result := False;
HSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if HSnapShot = 0 then exit;
ProcessEntry32.dwSize := sizeof(ProcessEntry32);
if Process32First(HSnapShot, ProcessEntry32) then
repeat
if CompareText(ProcessEntry32.szExeFile, ProcessName) = 0 then
begin
HProcess := OpenProcess(PROCESS_TERMINATE, False, ProcessEntry32.th32ProcessID);
if HProcess <> 0 then
begin
Result := TerminateProcess(HProcess, 0);
CloseHandle(HProcess);
end;
Break;
end;
until not Process32Next(HSnapShot, ProcessEntry32);
CloseHandle(HSnapshot);
end; |
Utilisation
KillProcess('MonExe.exe');
Partager