Some time ago a customer of mine was trying to add some global hooks (i.e. WH_CALLWNDPROC, WH_CALLWNDPROCRET, WH_SYSMSGFILTER, WH_GETMESSAGE) to his system by calling SetWindowsHookEx, but he kept getting error 126 or ERROR_MOD_NOT_FOUND. Those global hooks were included in the same dll. He had several other dlls with global hooks which hooks had been successfully added in the same system. If he restarted the system, he could use the problematic dll without problems, but them some of the other global hook dlls would end up failing with the same error. Why?
We debugged the issue and saw that we were hitting a limit on Windows: we can only use 32 global hook dlls with different name and/or path at a time (at least on Windows XP, Server 2003, Vista, Server 2008, Win7 and Server 2008 R2, x86 and x64). And if we reach that limit, only when we have unregistered all global hooks in a dll and all processes have unloaded that dll, we will be able to use a new global hook dll with a different name and/or path than those already loaded in the system.
According to MSDN:
The global hooks are a shared resource, and installing one affects all applications in the same desktop as the calling thread. All global hook functions must be in libraries. Global hooks should be restricted to special-purpose applications or to use as a development aid during application debugging.
So please, use global hooks carefully.
I hope this helps.
Alex (Alejandro Campos Magencio)