WinEvents: why delete old hook before generate a new hook?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Archimede
Posts: 533
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

WinEvents: why delete old hook before generate a new hook?

12 Apr 2023, 06:15

Hallo.
I am testing HookEvent function, downloaded from this site.
The function is:

Code: Select all

HookEvent( sFunctionName, aiEvents, sPID := "0", iFlags := "0" )
{
    Global sEventHookTable

    For i, Event_ID in aiEvents
    {
        If( !sEventHookTable[ Event_ID ] )
            sEventHookTable[ Event_ID ] := Array()
        If( CreateWinEventHook( sFunctionName, Event_ID, sPID, iFlags ))
            sEventHookTable[ Event_ID ].Push( sFunctionName )
    }
}

CreateWinEventHook( sFunctionName, iEvent, sPIDs := "0", dwFlags := "0" )
{
    Global _hHookTable

    cb := RegisterCallback( sFunctionName )
    DeleteWinEventHook( sFunctionName, iEvent )
    DllCall( "CoInitialize", UInt, 0 )
    If( hHook := DllCall( "SetWinEventHook", UInt, iEvent, UInt, iEvent, UInt, 0, UInt, cb, UInt, sPIDs, UInt, 0, UInt, dwFlags ))
        _hHookTable[ sFunctionName . "_" . iEvent ] := hHook

    Return !!hHook
}

DeleteWinEventHook( sFunctionName, iEvent )
{
    Global _hHookTable

    hHook := _hHookTable.Delete( sFunctionName . "_" . iEvent )

    Return ( hHook != 0 ? DllCall( "UnhookWinEvent", UInt, hHook ) : False )
}

My query is:
on the CreateWinEventHook(...) function, why use DeleteWinEventHook(...) to delete an old hook before generate a new hook?
Is no possible to generate a new hook wihout delete an old hook?
Thank you very much.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: WinEvents: why delete old hook before generate a new hook?

12 Apr 2023, 16:26

because whoever wrote this function thought it would be a good idea to delete old, existing hooks that target the same event constant
u can register as many hooks as ur process's current, remaining USER Handles count would permit, up to a limit of 10'000
Last edited by swagfag on 12 Apr 2023, 17:49, edited 1 time in total.
Archimede
Posts: 533
Joined: 25 Nov 2021, 09:49
Location: Switzerland / Italy

Re: WinEvents: why delete old hook before generate a new hook?

12 Apr 2023, 17:39

Ok, thank you very much.
The important thing is that it is no a technic necessity.
I am modifying it ant then I will post it there.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 96 guests