WinKill vs Process vs End Process
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
WinKill vs Process vs End Process
I have an application that fails to close gracefully so I'd like to just kill it using a hotkey. The End Process Button in Task Manager does this but neither WinKill nor Process will. How to simulate or activate the End Process button in the task manager? Thanks.
Re: WinKill vs Process vs End Process
use this, then execute winkill:
Code: Select all
VarSetCapacity(t, 4, 0), DllCall("Ntdll.dll\RtlAdjustPrivilege", "UInt", SeDebugPrivilege:=20, "UChar", Enabled:=true, "UChar", false, "Ptr", &t)
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
Re: WinKill vs Process vs End Process
Thanks, but that made no difference, WinKill behaved as before.
Re: WinKill vs Process vs End Process
Are you running the script as administrator?
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
Re: WinKill vs Process vs End Process
No, I'm using an account with non-Admin privileges.
Re: WinKill vs Process vs End Process
That's the problem, you couldn't terminate an elevated process (winkill do this if WM_CLOSE fail), from a non elevated process. You need to run your script process as admin.
try something like this:
Code: Select all
/*
hWnd: Window ID.
Force:
0 = WM_CLOSE.
1 = TerminateThread.
2 = TerminateProcess.
3 = if TerminateThread fails, try TerminateProcess.
*/
WinCloseEx(hWnd := "", Force := false) {
if !StrLen(hWnd + 0)
hWnd := WinExist(hWnd)
if (Force = 1) || (Force = 2) || (Force = 3) {
if !(ThreadId := DllCall("User32.dll\GetWindowThreadProcessId", "Ptr", hWnd, "UIntP", ProcessId, "UInt"))
return false
;maybe you could try terminate the thread...
if (Force = 1) || (Force = 3)
hThread := DllCall("Kernel32.dll\OpenThread", "UInt", 0x0001, "Int", false, "UInt", ThreadId, "Ptr")
, Result := DllCall("Kernel32.dll\TerminateThread", "Ptr", hThread, "UInt", 0)
, DllCall("Kernel32.dll\CloseHandle", "Ptr", hThread)
;this is how winkill works if WM_CLOSE fail.
if (Force = 2) || ((Force = 3) && !(Result))
hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0001, "Int", false, "UInt", ProcessId, "Ptr")
, Result := DllCall("Kernel32.dll\TerminateProcess", "Ptr", hProcess, "UInt", 0)
, DllCall("Kernel32.dll\CloseHandle", "Ptr", hProcess)
return Result
}
if !DllCall("User32.dll\IsWindow", "Ptr", hWnd) || DllCall("User32.dll\IsHungAppWindow", "Ptr", hWnd)
return false
;normal method
Result := DllCall("User32.dll\PostMessageW", "Ptr", hWnd, "UInt", 0x0002, "Ptr", 0, "Ptr", 0)
DllCall("User32.dll\PostMessageW", "Ptr", hWnd, "UInt", 0x112, "Ptr", 0xF060, "Ptr", 0)
return Result
}
;need run script as admin.
VarSetCapacity(t, 4, 0), DllCall("Ntdll.dll\RtlAdjustPrivilege", "UInt", SeDebugPrivilege:=20, "UChar", Enabled:=true, "UChar", false, "Ptr", &t)
;close.
MsgBox % WinCloseEx("ahk_class Notepad", 3)
Code: Select all
pid := {PROCESS ID}
Run *RunAs %A_WinDir%\System32\cmd.exe /c taskkill /f /pid %pid%,, hide
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
Re: WinKill vs Process vs End Process
Thanks, the application is started from the same normal account so I don't see how any process could be elevated. The End Process button in Task Manager just closes the application without asking for any extra privileges. I've tried using taskkill before starting the thread, sorry I should have mentioned that.
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
Re: WinKill vs Process vs End Process
Thanks for the link. However I've just realised that taskkill does work without admin privileges. I'd made a mistake with my code. Thanks for bearing with me while I sorted it out.
-
- Posts: 44
- Joined: 26 Jan 2017, 17:59
Re: WinKill vs Process vs End Process
You can find AHK V2 version here: viewtopic.php?f=83&t=126579
Re: WinKill vs Process vs End Process
I've noticed that OP never posted any script at any point during this thread. I recommend that if a user wants help with a script that does not work, the script should be posted so that other forum reader can examine it and provide specific feedback about it.
-
- Posts: 44
- Joined: 26 Jan 2017, 17:59
Re: WinKill vs Process vs End Process
You are right. That was misleading. I converted @Flipeador script WinCloseEx to AHK V2. I should have made that clearer. Sorry.
Re: WinKill vs Process vs End Process
Thanks. I meant for PuzzledGreatly, posting the original script would help.
-
- Posts: 44
- Joined: 26 Jan 2017, 17:59
Re: WinKill vs Process vs End Process
Defenitivly. But it is a very old thread. Let's see.
Re: WinKill vs Process vs End Process
Thank you!CyberKlabauter wrote: ↑27 Feb 2024, 03:54You can find AHK V2 version here: viewtopic.php?f=83&t=126579
Who is online
Users browsing this forum: No registered users and 75 guests