Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#4

Re: TerminateProcess not working correctly....

  Alt 22. Okt 2007, 09:52
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;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat