Help with unhooking WinEventHook Messages

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Help with unhooking WinEventHook Messages

25 Apr 2016, 19:44

Hi,

I'm having problems exiting and reloading a script that uses WinEventHook Messages from here:
https://autohotkey.com/board/topic/3266 ... -messages/

The script works well for its purpose (described below) but most of the time it crashes AutoHotkey ANSI 32 bit 1.1.23.01 (on Windows 7 64 bit) when I reload the script (by either pressing F8 or by double-clicking on the .ahk file if it's already running), and crashes randomly when I exit or Winclose the script. Can anyone offer any help as to why it would crash and also any potential fix? Could it be because of the version of AHK used (ANSI 32 bit), which I need to use for compatibility with other scripts?

The script is used with PowerPoint. If you have a slideshow running and add annotations with the pen pointer, when you exit the slideshow a window pops up asking if you want to Keep or Discard the annotations. This script detects this popup window and then will show a tooltip corresponding to the option selected (Kept! or Discarded!). As I mentioned, it works well for this purpose, but crashes on exit/reload.

Thanks in advance for any help you may be able to provide. If there is no easy fix using WinEventHook Messages, I'm also open to any other idea to capture the popup window and the selected option!

Cheers!

Code: Select all

;Shell hook to capture if ink annotations are kept or discarded
; https://autohotkey.com/board/topic/32662-tool-wineventhook-messages/

#SingleInstance, force
#NoEnv
#Persistent
SetBatchLines -1

HookProcAdr := RegisterCallback( "HookProc", "F" )
hWinEventHook := SetWinEventHook( 0x8, 0x8, 0, HookProcAdr, 0, 0, 0 )
OnExit, ExitSub
Return


ExitSub:
UnhookWinEvent()
ExitApp
Return

F8::
UnhookWinEvent()
sleep, 1000
Reload
Return


HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
Global
	if Event ; EVENT_SYSTEM_CAPTURESTART = 0x8
		{	
		IfWinActive, ahk_exe POWERPNT.EXE
			{
			WinGetClass, class, ahk_id %hWnd%
			WinGetTitle, Title, ahk_id %hWnd%
			If ((Class = "Button") and (Title = "&Discard"))
				Tooltip, Discarded!
						
			If ((Class = "Button") and (Title = "&Keep"))
				Tooltip, Kept!
			;Tooltip, 1_%Class%`n2_%hwnd%`n3_%idObject%`n4_%idChild%`n5_%dwEventThread%`n6_%dwmsEventTime%`n7_%Title%
			}
		}
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
	DllCall("CoInitialize", Uint, 0)
	return DllCall("SetWinEventHook"
	, Uint,eventMin	
	, Uint,eventMax	
	, Uint,hmodWinEventProc
	, Uint,lpfnWinEventProc
	, Uint,idProcess
	, Uint,idThread
	, Uint,dwFlags)	
}

UnhookWinEvent()
{
	Global
	DllCall( "UnhookWinEvent", Uint,hWinEventHook )
	DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Help with unhooking WinEventHook Messages

25 Apr 2016, 20:25

Hi,

Funnily enough, your script doesn't cause a crash here when running it with AutoHotkeyA32.exe on a 64-bit Windows 10 laptop. Since I can't actually confirm anything, were I guess the problem, I'd say it's the ampersand in your GlobalFree call. RegisterCallback already returns a pointer, which is what GlobalFree operates on; by using the address-of operator, you're passing a pointer to a pointer.

Other things to look at:
  • The second 0x8 in the SetWinEventHook call should be 0x9 (EVENT_SYSTEM_CAPTUREEND) and (totally optional) for the last argument, set it to 0x0002 so that the hook function isn't called for MsgBoxes etc. displayed by your own script
  • DllCall("CoInitialize", Uint, 0) should be removed - COM is already initialised by AutoHotkey_L
(Also, I really like cyruz's EWinHook library for this sort of thing: https://autohotkey.com/boards/viewtopic.php?t=830)
lblb
Posts: 190
Joined: 30 Sep 2013, 11:31

Re: Help with unhooking WinEventHook Messages

26 Apr 2016, 01:02

Hi qwerty12,
Once again, thanks a lot for your thoughtful reply. I still don't know what the problem was with the original version as your suggestions didn't lead to a more reliable script. However, cyruz's library works like a charm! So thanks a lot for the suggestion, it's really helped push my script forward. Cheers!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Tech Stuff, Vanda_a and 368 guests