DllCall - SetWindowsHookEx - WH_SHELL

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

DllCall - SetWindowsHookEx - WH_SHELL

04 Oct 2019, 08:00

Hey

I'm trying to replace the "SetWinEventHook" DllCall for "SetWindowsHookEx" to only register Top Window Creation events.

Reason:
Spoiler

So I've checked for RegisterShellHookWindow and prefered SetWindowsHookEx because the first is mentionned to possibly stop working in later Window versions. Quote Microsoft:
Spoiler

Can you please help me make that DllCall so that SetWindowsHookEx actualy works.
Here is what I have so far - which isn't working:

Code: Select all

		; Refer to:
		; 	- https://docs.microsoft.com/fr-fr/windows/win32/api/winuser/nf-winuser-setwindowshookexa
		; 	- https://www.autohotkey.com/boards/viewtopic.php?t=18813
		;	- https://autohotkey.com/board/topic/71347-setwindowshookex-fails/
		;
		; Used Microsoft constants in this DllCall :
		;  	- WH_SHELL := 10
		;
		; PS: my system is a 64-bits process
		hWinEventHook := DllCall("SetWindowsHookEx"
			, "Int", 10	; idHook of type int := WH_SHELL := 10
			, "Ptr", RegisterCallback("OnWH_SHELL") ; lpfn of type HOOKPROC ???
			, "Ptr", 0 ; hmod of type HINSTANCE ???
			, "Ptr", ProcessID) ; dwThreadId of type DWORD ???
			
;--------------------------------------------------------------------------------
OnWM_SHELL(nCode, wParam, lParam) {
;--------------------------------------------------------------------------------
	
	; Handle Callbacks on Sage.exe ProcessID
	Tooltip %A_TickCount%`n%nCode%`n%wParam%`n%lParam%
	
}
For reference here's the working SetWinEventHook that I want to replace:
Spoiler
Thanks
Last edited by DRocks on 04 Oct 2019, 16:11, edited 2 times in total.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: DllCall - SetWindowsHookEx - OnWH_SHELL

04 Oct 2019, 11:32

DRocks wrote: Can you please help me make that DllCall so that SetWindowsHookExactualy works.
You can't use SetWindowsHookEx with WH_SHELL in pure AHK since the callback function must be placed in a dll in this case (if you want to monitor windows belonging to the external processes).
DRocks wrote: How I am currently doing it is with the SetWinEventHook that looks for EVENT_OBJECT_CREATE
but I do not like the 300+ callbacks that happen regularly with this function.
300+ callbacks won't happen, if you write the code properly. For example, MsgBox will appear only if the Notepad window is created:

Code: Select all

#Persistent
DetectHiddenWindows, On
EVENT_OBJECT_CREATE := 0x8000
hHook := SetWinEventHook( EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, 0, RegisterCallback("HookProc", "F"), 0, 0, 0 )
OnExit( Func("FreeResource").Bind(hHook) )
Return

FreeResource(hHook) {
   DllCall("UnhookWindowsHookEx", "Ptr", hHook)
}

HookProc(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
   static OBJID_WINDOW := 0
   if (idObject != OBJID_WINDOW)
      Return
   WinGetClass, winClass, ahk_id %hwnd%
   if (winClass != "Notepad")
      Return
   MsgBox, 4096, % " ", Notepad window has been created!
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
   return DllCall("SetWinEventHook", "UInt", eventMin, "UInt", eventMax, "Ptr", hmodWinEventProc
                                   , "Ptr", lpfnWinEventProc, "UInt", idProcess, "UInt", idThread, "UInt", dwFlags, "Ptr")
}
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: DllCall - SetWindowsHookEx - OnWH_SHELL

04 Oct 2019, 12:16

teadrinker wrote:
04 Oct 2019, 11:32
300+ callbacks won't happen, if you write the code properly. For example, MsgBox will appear only if the Notepad window is created:
Hey @teadrinker Thank you very much for your reply.
This has indeed helped reduce the number of callbacks that passthrough the "returns"
Thank you very much.

Side question: you have used the "Fast" keyword in your DllCall, why are you using it. What are the benefits?
I've tried it before and it seemed to miss some calls and was not properly trigerring the functions I needed to trigger inside the callback

Here's my sample

Code: Select all

;--------------------------------------------------------------------------------
OnEVENT_OBJECT_CREATE(hWinEventHook, event, hWnd, idObject) {
;--------------------------------------------------------------------------------
	
	; Handle Callbacks for = EVENT_OBJECT_CREATE (0x8000) on Sage.exe ProcessID
	static OBJID_WINDOW := 0
	static index
	
	if (idObject != OBJID_WINDOW)
		Return, 0
	
	WinGetClass, winClass, ahk_id %hWnd%
	if (!InStr(winClass , "WindowsForms10.Window.8.app.0."))
		Return, 0 ; only target Sage Top Window classes
	
	index++
	tooltip(index "`n" winClass)
	
	if ((!WinExist(Achats["hChildGui"])) and (hParent:=WinExist(WinTitles["Achats"]))) {
		CreateAchatsChildGui(hParent)
	}
}
I am still interested in the WindowsHookEx though but id does seem very complex.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: DllCall - SetWindowsHookEx - OnWH_SHELL

04 Oct 2019, 13:42

DRocks wrote: Side question: you have used the "Fast" keyword in your DllCall, why are you using it. What are the benefits?
You could read about it here.
Perhaps, it this case it's safer not to use fast mode, although for me it doesn't cause any issues.
DRocks wrote: I am still interested in the WindowsHookEx though but id does seem very complex.
You need someone, who will write the dll for you in c++. :)
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: DllCall - SetWindowsHookEx - OnWH_SHELL

04 Oct 2019, 14:23

teadrinker wrote:
04 Oct 2019, 13:42
hehe :D
the so awesome c++ (wish I would know that language)
Thanks for the help! it is already like 50% more efficient with your addition :D
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: DllCall - SetWindowsHookEx - WH_SHELL

05 Oct 2019, 01:13

teadrinker wrote: You need someone, who will write the dll for you in c++
I found for you one person. For me his code works.
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: DllCall - SetWindowsHookEx - WH_SHELL

05 Oct 2019, 09:43

I read through most it and it looks to be perfect !
Ill try it out. Thanks again @teadrinker - very nice of you

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: MrHue, Rohwedder, Rxbie, songdg and 353 guests