Microsoft KB Archive/74547

{|
 * width="100%"|

INFO: Importance of Calling DefHookProc

 * }

Q74547

-

The information in this article applies to:


 * Microsoft Win32 Software Development Kit (SDK)
 * Microsoft Windows Software Development Kit (SDK) versions 3.0, 3.1

-

SUMMARY
When an application installs a hook using SetWindowsHook, Windows adds the hook's callback filter function to the hook chain. It is the responsibility of each callback function to call the next function in the chain. DefHookProc is used to call the next function in the hook chain for Windows 3.0. DefHookProc is retained in Windows 3.1 for backwards compatibility. For Windows 3.1, you should use CallNextHookEx to call the next function in the hook chain.

For Win32, mouse and keyboard hooks can suppress messages by return value and do not have to call CallNextHookEx, unless they want to pass the message on. Other hooks, like WH_CALLWNDPROC, don't need to call CallNextHookEx, because it will be called by the system. However, all hooks should call CallNextHookEx immediately if nCode<0.

Windows 3.0
If a callback function does not call DefHookProc, none of the filter functions that were installed before the current filter will be called. Windows will try to process the message and this could hang the system.

Only a keyboard hook (WH_KEYBOARD) can suppress a keyboard event by not calling DefHookProc and returning a 1. When the system gets a value of 1 from a keyboard hook callback function, it discards the message.

Windows 3.1
In Windows 3.1, the WH_MOUSE hook will work like the WH_KEYBOARD hook in that the mouse event can be suppressed by returning 1 instead of calling DefHookProc.

Furthermore, when the hook callback function receives a negative value for the nCode parameter, it should pass the message and the parameters to DefHookProc without further processing. When nCode is negative, Windows is in the process of removing a hook callback function from the hook chain.

Additional query words:

Keywords : kbHook kbOSWinNT kbOSWin2000 kbSDKWin32 kbGrpDSUser kbOSWin

Issue type : kbinfo

Technology : kbWin32SDKSearch kbAudDeveloper kbWin3xSearch kbSDKSearch kbWin32sSearch kbWinSDKSearch kbWinSDK300 kbWinSDK310