PRB: SetWindowsHookEx() Fails to Install Task-Specific Filter

ID: Q92659

The information in this article applies to:

SYMPTOMS

In Windows version 3.1, the SetWindowsHookEx() function fails when it is called to install a task-specific filter (hook) that resides in a DLL.

CAUSE

According to the documentation, the third parameter to the SetWindowsHookEx() function must be the instance handle of the application or the DLL that contains the filter function. However, because of a problem in Windows 3.1, the SetWindowsHookEx() function fails when it is called to install a task-specific filter using the DLL's instance handle.

Note that such a problem does not exist when the SetWindowsHookEx() function is called to install a system-wide filter in a DLL. The DLL's instance handle is accepted as a valid parameter. The first argument passed to the LibMain function of a DLL contains its instance handle.

RESOLUTION

To install a task-specific filter that resides in a DLL, pass the module handle of the DLL as the third parameter to the SetWindowsHookEx() function. The module handle can be retrieved using the GetModuleHandle() function. For example, to install a task-specific keyboard filter, the code might resemble the following:

   g_hHook = SetWindowsHookEx( WH_KEYBOARD,
                   HookCallbackProc,
                   GetModuleHandle( "HOOK.DLL" ),
                   hTargetTask );

This resolution is compatible with future versions of Windows.

Additional query words: hook not allowed

Keywords          : kbcode kb16bitonly kbHook kbGrpUser kbWinOS310 
Issue type        : kbprb

Last Reviewed: December 26, 1998