Reputation: 629
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
Reputation: 151
I believe you might lack the SYNCHRONIZE privilege for the process you want to terminate.
in order to verify that:
This will succeed because you have the SYNCHRONIZE privilege on a process you have created. (If not - this is a different issue)
Upvotes: 4