TerminateProcess returns immediately no matter whether the process was killed or not. You have to verify the terminations of the process. And if you get a valid process
handle the process is still alive.
Delphi-Quellcode:
{*
* Procedure : KillProcess
* Author : Michael Puff
* Date : 2006-09-15
* Terminates a process identified by its PID
*}
function KillProcess(dwProcID, Wait: DWORD): Integer;
var
hProcess : Cardinal;
dw : DWORD;
begin
// open the process and store the process-handle
hProcess := OpenProcess(SYNCHRONIZE
or PROCESS_TERMINATE, False, dwProcID);
// kill it
if hProcess <> 0
then
begin
dw := Integer(TerminateProcess(hProcess, 1));
if dw <> 0
then
begin
// TerminateProcess returns immediately, so we have to verify the result via
// WaitForSingleObject
dw := WaitForSingleObject(hProcess, Wait);
if dw = WAIT_FAILED
then
dw := GetLastError;
end
else // TerminateProcess = 0
dw := GetLastError;
CloseHandle(hProcess);
end
else // hProcess = INVALID_HANDLE_VALUE
dw := GetLastError;
result := dw;
end;