After reading the posts by
qwerty12, I remembered coming across an interesting unexpected behaviour in a function I wrote some time back (being inspired by forum folks, one of which was
HotkeyIt). After checking back with our
quizzmaster Helgef I now present my puzzle in the form of two functions (the first of which is buggy IMHO), and a use-case for the two functions that demonstrates the unexpected behaviour:
Code: Select all
;-------------------------------------------------------------------------------
Watchdog(wParam, lParam) { ; a self-contained ShellHook function
;-------------------------------------------------------------------------------
static init := DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)
, MsgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
, neglect := OnMessage(MsgNum, "Watchdog")
, CleanUp := {base: {__Delete: "Watchdog"}}
If Not IsObject(CleanUp) {
OnMessage(MsgNum, "")
DllCall("DeregisterShellHookWindow", "Ptr", A_ScriptHwnd)
}
If (wParam = 1) ; HSHELL_WINDOWCREATED
Or (wParam = 4) ; HSHELL_WINDOWACTIVATED
Or (wParam = 32772) ; HSHELL_RUDEAPPACTIVATED (fullscreen app activated)
Guard(lParam) ; lParam is the active window's handle
}
;-------------------------------------------------------------------------------
Guard(hWnd) { ; do something with the new active window
;-------------------------------------------------------------------------------
; code
}
Code: Select all
SetBatchLines, -1
Return ; end of auto-execute section
;-------------------------------------------------------------------------------
Watchdog(wParam, lParam) { ; a self-contained ShellHook function
;-------------------------------------------------------------------------------
static init := DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)
, MsgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
, neglect := OnMessage(MsgNum, "Watchdog")
, CleanUp := {base: {__Delete: "Watchdog"}}
If Not IsObject(CleanUp) {
MsgBox, CleanUp is being called! ; temporary line added for debugging
OnMessage(MsgNum, "")
DllCall("DeregisterShellHookWindow", "Ptr", A_ScriptHwnd)
}
If (wParam = 1) ; HSHELL_WINDOWCREATED
Or (wParam = 4) ; HSHELL_WINDOWACTIVATED
Or (wParam = 32772) ; HSHELL_RUDEAPPACTIVATED (fullscreen app activated)
Guard(lParam) ; lParam is the active window's handle
}
;-------------------------------------------------------------------------------
Guard(hWnd) { ; do something with the new active window
;-------------------------------------------------------------------------------
; this example displays a ToolTip with some info
WinGetTitle, Title, ahk_id %hWnd%
WinGetClass, Class, ahk_id %hWnd%
ToolTip, %Title%`n%Class%
SetTimer, ToolTipOff, -2000
Return
ToolTipOff:
ToolTip ; off
Return
}
I would expect to see a MsgBox when I exit the script, but I see none. The whole
CleanUp will not get called, unless the bug is fixed.
The puzzle is: Can someone fix the bug?
Good luck and have fun!