uses tlhelp32, ShellAPI
function GetProcessID(Exename: string): DWORD; - benötigt tlhelp32 und ShellAPI in uses!
var
hProcSnap: THandle;
pe32: TProcessEntry32;
begin
result := 0;
hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
if hProcSnap <> INVALID_HANDLE_VALUE then
begin
pe32.dwSize := SizeOf(ProcessEntry32);
if Process32First(hProcSnap, pe32) = true then
begin
while Process32Next(hProcSnap, pe32) = true do
begin
if pos(Exename, pe32.szExeFile) <> 0 then
result := pe32.th32ProcessID;
end;
end;
CloseHandle(hProcSnap);
end;
end;
function KillProcess(PID: DWord): Bool;
var
hProcess: THandle;
begin
hProcess := OpenProcess(PROCESS_TERMINATE, False, PID);
Result := TerminateProcess(hProcess, 0);
end;
Beispiel :
procedure TMainForm.SpeedButton17Click(Sender: TObject);
begin
KillProcess(GetProcessID('my.exe'));
end;
Keine Kommentare:
Kommentar veröffentlichen