{******************************************************************************}
{** **}
{** ProzessID an Hand der Exe-Datei ermittlen **}
{** **}
{******************************************************************************}
function GetProcessID(sProcName:
String): Integer;
var
hProcSnap: THandle;
pe32: TProcessEntry32;
begin
result := -1;
hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
if hProcSnap = INVALID_HANDLE_VALUE
then exit;
pe32.dwSize := SizeOf(ProcessEntry32);
{ wenn es geklappt hat }
if Process32First(hProcSnap, pe32) = true
then
{ und los geht's: Prozess suchen}
while Process32Next(hProcSnap, pe32) = true
do
begin
if pos(sProcName, pe32.szExeFile) <> 0then
result := pe32.th32ProcessID;
end;
CloseHandle(hProcSnap);
end;
{******************************************************************************}
{** **}
{** Prozess beenden **}
{** **}
{******************************************************************************}
procedure KillProcess(dwProcID: DWORD);
var
hProcess : Cardinal;
dw : DWORD;
begin
{ open the process and store the process-handle }
hProcess := OpenProcess(SYNCHRONIZE
or PROCESS_TERMINATE, False, dwProcID);
{ kill it }
TerminateProcess(hProcess, 0);
{ TerminateProcess returns immediately, so wie have to verify the result via
WaitfForSingleObject }
dw := WaitForSingleObject(hProcess, 5000);
case dw
of
{ everythings's all right, we killed the process }
WAIT_OBJECT_0: Messagebox(Application.Handle, '
Prozess wurde beendet.', '
Prozess beenden',
MB_ICONINFORMATION);
{ process could not be terminated after 5 seconds }
WAIT_TIMEOUT:
begin
Messagebox(Application.Handle, '
Prozess konnte nicht innerhalb von 5 Sekunden beendet werden.',
'
Prozess beenden', MB_ICONSTOP);
CloseHandle(hProcess);
exit;
end;
{ error in calling WaitForSingleObject }
WAIT_FAILED:
begin
RaiseLastOSError;
CloseHandle(hProcess);
exit;
end;
end;
CloseHandle(hProcess);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
KillProcess(GetProcessID(edit1.text));
// in edit1 muss zb icq.exe stehen
end;