How memory for the hook is freed depends on the type of hook chain an event is linked to:
A queue hook chain is a private hook chain. It applies only to the current calling thread that created the queue with which the hook chain is associated. It may or may not reside in a DLL. If it is not associated with a DLL, its memory can be freed by WinReleaseHook, as shown in the previous sample code.
A system hook chain must reside in a DLL; therefore, it affects the entire system. WinSetHook allocates memory and associates it with a DLL. This memory is not freed until the DLL module is freed. WinReleaseHook cannot free the DLL's memory, because another process cannot free the DLL and its associated memory. However, this memory can be freed by launching a thread that does the following:
As long as any DLL associated with the hook is alive, WinReleaseHook cannot free the memory.
The implication here is straightforward:
The following sample code shows a function, called from an application's main routine, that releases the hook and frees the memory of the hook installed in a sample code shown earlier.
/***********************************************************************//* StopInputHook: This function stops the hook filtering. */ /***********************************************************************/ void EXPENTRY StopInputHook(void) { /***********************************************************************/ /* Drop a hook to our input filter routine. */ /***********************************************************************/ WinReleaseHook(habDLL, NULLHANDLE, HK_INPUT, pfnInput, hMod); /***********************************************************************/ /* Decrement the DLL usage count. */ /***********************************************************************/ DosFreeModule(hMod); }