SetWindowsHookEx fails with error ERROR_MOD_NOT_FOUND

Hi all,

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)

Comments (2)
  1. Thanks a lot and i have doubt on Global hooks,

    Is is possible to run 32bit CBT hook and 64bit mouse hook together on windows 7(64bit) ?

    (both are global hook)

    Because am getting issue on that. i have installed 32bit hook and 64bit hook. after installation of 64bit hook, if i start any 32bit window application, it is not responding and unhooked also. if i kill that 64bit hook process, 32bit hook start to work.

    please give me some information regarding this..

  2. Hi Raj,

    I haven't seen that issue before so I'm afraid I don't know the answer right away. If you need assistance, please open a case with Microsoft Technical Support so we can investigate this properly.



Comments are closed.

Skip to main content