CallNextHookEx function (Windows)

Switch View :
ScriptFree
CallNextHookEx function

Applies to: desktop apps only

Passes the hook information to the next hook procedure in the current hook chain. A hook procedure can call this function either before or after processing the hook information.

Syntax

LRESULT WINAPI CallNextHookEx(
  __in_opt  HHOOK hhk,
  __in      int nCode,
  __in      WPARAM wParam,
  __in      LPARAM lParam
);

Parameters

hhk [in, optional]

Type: HHOOK

This parameter is ignored.

nCode [in]

Type: int

The hook code passed to the current hook procedure. The next hook procedure uses this code to determine how to process the hook information.

wParam [in]

Type: WPARAM

The wParam value passed to the current hook procedure. The meaning of this parameter depends on the type of hook associated with the current hook chain.

lParam [in]

Type: LPARAM

The lParam value passed to the current hook procedure. The meaning of this parameter depends on the type of hook associated with the current hook chain.

Return value

Type:

Type: LRESULT

This value is returned by the next hook procedure in the chain. The current hook procedure must also return this value. The meaning of the return value depends on the hook type. For more information, see the descriptions of the individual hook procedures.

Remarks

Hook procedures are installed in chains for particular hook types. CallNextHookEx calls the next hook in the chain.

Calling CallNextHookEx is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
SetWindowsHookEx
UnhookWindowsHookEx
Conceptual
Hooks

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Community Content

Юраш
Handle to the current hook from the .exe?
How exactly is this supposed to work? I need to pass a handle to the current hook, but the hook is in a dll and the handle to the hook is in the exe. What am I missing? It would be nice if the example code for hooks showed a dll being used instead of everything wrapped into a single exe.

- You DON'T need to pass a handle to the current hook to CallNextHookEx. "This parameter is ignored", as stated above. You can pass NULL for the first argument.

Юраш
Timeout and CallNextHookEx Function
For WH_MOUSE_LL hook there is a timeout (HKEY_CURRENT_USER\Control Panel\Desktop\LowLevelHooksTimeout value in the Registry). If my hook procedure exeeds this limit, in some Windows versions it will be excluded from the hook chain.
  1. But if I call CallNextHookEx function in my LowLevelMouseProc, and there all subsequent hooks will be executed, how the OS will know which hook is guilty of overtime? Is it so that CallNextHookEx somehow mark the last hook executed, to "pass the message to the next hook" "if the hook procedure does not return during this interval"? How does OS measure hook execution time in case when it only place message to the hook thread queue (as in WH_MOUSE_LL)?
  2. Is it somehow advantageous for me not to call CallNextHookEx to prevent other (sometimes badly performing) hooks to spoil my functionality? Or is it so that in WH_MOUSE_LL case, CallNextHookEx execute time is small and constant, because it only posts a message to the next hook's thread message queue?

dmex
C# syntax
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr CallNextHookEx(HandleRef hhook, int code, IntPtr wparam, IntPtr lparam);

dmex
VB.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function CallNextHookEx(ByVal hhook As HandleRef, ByVal code As Integer, ByVal wparam As IntPtr, ByVal lparam As IntPtr) As IntPtr
End Function

Simon Said
Empty "Return Value" section
The section "Return Value" does not say anything about the expected return values and their meaning.