Increase the SetWindowsHookEx limit in Windows 7

It appears that Windows 7 (some say also Windows Vista) enforces a limit to the number of times a hook can be placed through SetWindowsHookEx.

I’m writing a Java application and using jnativehook to get the keyboard and mouse hooks to the system (in order to track their activity outside the application). The issue is, after about a 100 times of doing so, the procedure to get hooks simply stops working – silently (with no exception thrown).

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;

static void listenerInit() throws NativeHookException {

    GlobalScreen.registerNativeHook();
    // add listeners

}

I tried to look for a solution to increase this limit, so that I don’t need to keep restarting the computer so often just to reset the counter. All I got so far is a suggestion to add a registry key to increase the timeout to 10 seconds, but that doesn’t sound right.

HKEY_CURRENT_USERControl PanelDesktop
LowLevelHooksTimeout=10000
// why do this?

Apparently, this is not ideal for a developer as it’s a user-oriented workaround for slow applications that use hooks. But I am only testing the application by terminating it and running it repeatedly. In a deployed environment, it would only run once at start up. Will this work for my test system?

How can I increase this limit? Also I don’t understand the relation between the timeout value and the reason why more new hooks cannot be created after the limit is reached.

Please note that I’m only interested in either a Java-based solution, or a solution involving the modification of the test system OS configuration (Windows 7 Professional). I’ve seen some suggestions involving writing some C++ or C# libraries that I’m not interested in.

Also, will this be solved if I upgrade to Windows 10?


Source: windows

Leave a Reply