Atak_Snajpera
Atak_Snajpera

Reputation: 629

WaitForSingleObject always returns WAIT_FAILED

Why WaitForSingleObject function in this case always returns WAIT_FAILED?

function KillProcessByWindowName(WindowTitle:string):boolean;
var WindowHandle:HWND;
    PID: DWORD;
    ProcessHandle,WaitStatus:cardinal;
begin
  Result:=false;
  WindowHandle:=FindWindow(nil,PChar(WindowTitle));
  if WindowHandle<>0 then
  begin
    GetWindowThreadProcessID(WindowHandle,@PID);
    if PID<>0 then
    begin
      ProcessHandle:=OpenProcess(PROCESS_TERMINATE,False,PID);
      if ProcessHandle>0 then
      begin
        TerminateProcess(ProcessHandle,0);
        repeat
          WaitStatus:=WaitForSingleObject(ProcessHandle,100);
        until WaitStatus<>WAIT_TIMEOUT;

        if WaitStatus=WAIT_OBJECT_0 then Result:=true
      end;
      CloseHandle(ProcessHandle);
    end;
  end;

end;

Upvotes: 0

Views: 707

Answers (1)

Erez Amir
Erez Amir

Reputation: 151

I believe you might lack the SYNCHRONIZE privilege for the process you want to terminate.
in order to verify that:

  1. create a process P inside your app and grab its handle
  2. while still in the same application, call KillProcessByWindowName() on P

This will succeed because you have the SYNCHRONIZE privilege on a process you have created. (If not - this is a different issue)

Upvotes: 4

Related Questions