WinKill vs Process vs End Process

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

WinKill vs Process vs End Process

02 Dec 2016, 17:18

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.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: WinKill vs Process vs End Process

02 Dec 2016, 18:31

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)
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: WinKill vs Process vs End Process

02 Dec 2016, 20:31

Thanks, but that made no difference, WinKill behaved as before.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: WinKill vs Process vs End Process

02 Dec 2016, 23:26

Are you running the script as administrator?
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: WinKill vs Process vs End Process

03 Dec 2016, 03:51

No, I'm using an account with non-Admin privileges.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: WinKill vs Process vs End Process

03 Dec 2016, 05:42

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.

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)
try something like this:

Code: Select all

pid := {PROCESS ID}

Run *RunAs %A_WinDir%\System32\cmd.exe /c taskkill /f /pid %pid%,, hide
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: WinKill vs Process vs End Process

03 Dec 2016, 17:26

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.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: WinKill vs Process vs End Process

04 Dec 2016, 03:19

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.
User avatar
mikeyww
Posts: 26991
Joined: 09 Sep 2014, 18:38

Re: WinKill vs Process vs End Process

27 Feb 2024, 05:14

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.
CyberKlabauter
Posts: 44
Joined: 26 Jan 2017, 17:59

Re: WinKill vs Process vs End Process

27 Feb 2024, 08:34

You are right. That was misleading. I converted @Flipeador script WinCloseEx to AHK V2. I should have made that clearer. Sorry.
User avatar
mikeyww
Posts: 26991
Joined: 09 Sep 2014, 18:38

Re: WinKill vs Process vs End Process

27 Feb 2024, 08:39

Thanks. I meant for PuzzledGreatly, posting the original script would help. :thumbup:
CyberKlabauter
Posts: 44
Joined: 26 Jan 2017, 17:59

Re: WinKill vs Process vs End Process

27 Feb 2024, 11:38

Defenitivly. But it is a very old thread. Let's see.
User avatar
andymbody
Posts: 906
Joined: 02 Jul 2017, 23:47

Re: WinKill vs Process vs End Process

29 Feb 2024, 06:00

CyberKlabauter wrote:
27 Feb 2024, 03:54
You can find AHK V2 version here: viewtopic.php?f=83&t=126579
Thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 75 guests