Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions sdk-api-src/content/winuser/nf-winuser-setwindowshookexa.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
UID: NF:winuser.SetWindowsHookExA
title: SetWindowsHookExA function (winuser.h)
description: Installs an application-defined hook procedure into a hook chain. (ANSI)
helpviewer_keywords: ["SetWindowsHookExA", "WH_CALLWNDPROC", "WH_CALLWNDPROCRET", "WH_CBT", "WH_DEBUG", "WH_FOREGROUNDIDLE", "WH_GETMESSAGE", "WH_JOURNALPLAYBACK", "WH_JOURNALRECORD", "WH_KEYBOARD", "WH_KEYBOARD_LL", "WH_MOUSE", "WH_MOUSE_LL", "WH_MSGFILTER", "WH_SHELL", "WH_SYSMSGFILTER", "winuser/SetWindowsHookExA"]
helpviewer_keywords: ["SetWindowsHookEx", "SetWindowsHookEx function [Windows and Messages]", "SetWindowsHookExA", "WH_CALLWNDPROC", "WH_CALLWNDPROCRET", "WH_CBT", "WH_DEBUG", "WH_FOREGROUNDIDLE", "WH_GETMESSAGE", "WH_JOURNALPLAYBACK", "WH_JOURNALRECORD", "WH_KEYBOARD", "WH_KEYBOARD_LL", "WH_MOUSE", "WH_MOUSE_LL", "WH_MSGFILTER", "WH_SHELL", "WH_SYSMSGFILTER", "_win32_SetWindowsHookEx", "_win32_setwindowshookex_cpp", "winmsg.setwindowshookex", "winui._win32_setwindowshookex", "winuser/SetWindowsHookEx", "winuser/SetWindowsHookExA"]
old-location: winmsg\setwindowshookex.htm
tech.root: winmsg
ms.assetid: VS|winui|~\winui\windowsuserinterface\windowing\hooks\hookreference\hookfunctions\setwindowshookex.htm
Expand Down Expand Up @@ -40,6 +40,7 @@ topic_type:
api_type:
- DllExport
api_location:
- ext-ms-win-rtcore-ntuser-window-ext-l1-1-1.dll
- ext-ms-win-ntuser-window-l1-1-6.dll
- ext-ms-win-ntuser-window-l1-1-5.dll
- User32.dll
Expand All @@ -59,7 +60,6 @@ req.apiset: ext-ms-win-ntuser-window-l1-1-0 (introduced in Windows 8)

# SetWindowsHookExA function


## -description

Installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.
Expand All @@ -70,7 +70,7 @@ Installs an application-defined hook procedure into a hook chain. You would inst

Type: <b>int</b>

The type of hook procedure to be installed. This parameter can be one of the following values.
The type of hook procedure to be installed. This parameter can be one of the following values.

<table>
<tr>
Expand All @@ -96,7 +96,7 @@ Installs a hook procedure that monitors messages before the system sends them to
</dl>
</td>
<td width="60%">

Installs a hook procedure that monitors messages after they have been processed by the destination window procedure. For more information, see the [HOOKPROC callback function](nc-winuser-hookproc.md) hook procedure.

</td>
Expand All @@ -108,7 +108,7 @@ Installs a hook procedure that monitors messages after they have been processed
</dl>
</td>
<td width="60%">

Installs a hook procedure that receives notifications useful to a CBT application. For more information, see the [CBTProc](/windows/win32/winmsg/cbtproc) hook procedure.

</td>
Expand All @@ -133,7 +133,7 @@ Installs a hook procedure useful for debugging other hook procedures. For more i
</td>
<td width="60%">

Installs a hook procedure that will be called when the application's foreground thread is about to become idle. This hook is useful for performing low priority tasks during idle time. For more information, see the [*ForegroundIdleProc*](/windows/win32/winmsg/foregroundidleproc) hook procedure.
Installs a hook procedure that will be called when the application's foreground thread is about to become idle. This hook is useful for performing low priority tasks during idle time. For more information, see the [ForegroundIdleProc](/windows/win32/winmsg/foregroundidleproc) hook procedure.

</td>
</tr>
Expand All @@ -145,7 +145,7 @@ Installs a hook procedure that will be called when the application's foreground
</td>
<td width="60%">

Installs a hook procedure that monitors messages posted to a message queue. For more information, see the [*GetMsgProc*](/windows/win32/winmsg/getmsgproc) hook procedure.
Installs a hook procedure that monitors messages posted to a message queue. For more information, see the [GetMsgProc](/windows/win32/winmsg/getmsgproc) hook procedure.

</td>
</tr>
Expand All @@ -158,7 +158,7 @@ Installs a hook procedure that monitors messages posted to a message queue. For
<td width="60%">

> [!WARNING]
> **Windows 11 and newer**: Journaling hook APIs are not supported. We recommend using the [**SendInput**](/windows/win32/api/winuser/nf-winuser-sendinput) TextInput API instead.
> Journaling Hooks APIs are unsupported starting in Windows 11 and will be removed in a future release. Because of this, we highly recommend calling the [**SendInput**](/windows/win32/api/winuser/nf-winuser-sendinput) TextInput API instead.

Installs a hook procedure that posts messages previously recorded by a [WH_JOURNALRECORD](/windows/desktop/winmsg/about-hooks) hook procedure. For more information, see the [JournalPlaybackProc](/windows/win32/winmsg/journalplaybackproc) hook procedure.

Expand All @@ -173,7 +173,8 @@ Installs a hook procedure that posts messages previously recorded by a [WH_JOURN
<td width="60%">

> [!WARNING]
> **Windows 11 and newer**: Journaling hook APIs are not supported. We recommend using the [**SendInput**](/windows/win32/api/winuser/nf-winuser-sendinput) TextInput API instead.
> Journaling Hooks APIs are unsupported starting in Windows 11 and will be removed in a future release. Because of this, we highly recommend calling the [**SendInput**](/windows/win32/api/winuser/nf-winuser-sendinput) TextInput API instead.


Installs a hook procedure that records input messages posted to the system message queue. This hook is useful for recording macros. For more information, see the [JournalRecordProc](/windows/win32/winmsg/journalrecordproc) hook procedure.

Expand All @@ -198,8 +199,8 @@ Installs a hook procedure that monitors keystroke messages. For more information
</dl>
</td>
<td width="60%">
Installs a hook procedure that monitors low-level keyboard input events. For more information, see the <a href="/windows/win32/winmsg/lowlevelkeyboardproc">LowLevelKeyboardProc</a> hook procedure.

Installs a hook procedure that monitors low-level keyboard input events. For more information, see the [LowLevelKeyboardProc](/windows/win32/winmsg/lowlevelkeyboardproc) hook procedure.

</td>
</tr>
Expand Down Expand Up @@ -246,7 +247,7 @@ Installs a hook procedure that monitors messages generated as a result of an inp
</dl>
</td>
<td width="60%">

Installs a hook procedure that receives notifications useful to shell applications. For more information, see the [ShellProc](/windows/win32/winmsg/shellproc) hook procedure.

</td>
Expand Down Expand Up @@ -287,7 +288,7 @@ The identifier of the thread with which the hook procedure is to be associated.

Type: <b>HHOOK</b>

If the function succeeds, the return value is the handle to the hook procedure.
If the function succeeds, the return value is the handle to the hook procedure.

If the function fails, the return value is <b>NULL</b>. To get extended error information, call [GetLastError](/windows/desktop/api/errhandlingapi/nf-errhandlingapi-getlasterror).

Expand All @@ -301,13 +302,15 @@ If a 64-bit application installs a global hook on 64-bit Windows, the 64-bit hoo

To hook all applications on the desktop of a 64-bit Windows installation, install a 32-bit global hook and a 64-bit global hook, each from appropriate processes, and be sure to keep pumping messages in the hooking application to avoid blocking normal functioning. If you already have a 32-bit global hooking application and it doesn't need to run in each application's context, you may not need to create a 64-bit version.

An error may occur if the <i>hMod</i> parameter is <b>NULL</b> and the <i>dwThreadId</i> parameter is zero or specifies the identifier of a thread created by another process.
An error may occur if the <i>hMod</i> parameter is <b>NULL</b> and the <i>dwThreadId</i> parameter is zero or specifies the identifier of a thread created by another process.

Calling the [CallNextHookEx function](nf-winuser-callnexthookex.md) function to chain to the next hook procedure 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 <b>CallNextHookEx</b> unless you absolutely need to prevent the notification from being seen by other applications.
Calling the [CallNextHookEx function](nf-winuser-callnexthookex.md) function to chain to the next hook procedure 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 <b>CallNextHookEx</b> unless you absolutely need to prevent the notification from being seen by other applications.

Before terminating, an application must call the [UnhookWindowsHookEx function](nf-winuser-unhookwindowshookex.md) function to free system resources associated with the hook.
In .NET apps, you must ensure the callback is not moved around by the garbage collector (otherwise your app will crash with an ExecutionEngineException). One way to do this is by making the callback a static method of your class.

The scope of a hook depends on the hook type. Some hooks can be set only with global scope; others can also be set for only a specific thread, as shown in the following table.
Before terminating, an application must call the [UnhookWindowsHookEx function](nf-winuser-unhookwindowshookex.md) function to free system resources associated with the hook.

The scope of a hook depends on the hook type. Some hooks can be set only with global scope; others can also be set for only a specific thread, as shown in the following table.

<table class="clsStd">
<tr>
Expand Down Expand Up @@ -378,9 +381,9 @@ The scope of a hook depends on the hook type. Some hooks can be set only with gl

For a specified hook type, thread hooks are called first, then global hooks. Be aware that the WH_MOUSE, WH_KEYBOARD, WH_JOURNAL*, WH_SHELL, and low-level hooks can be called on the thread that installed the hook rather than the thread processing the hook. For these hooks, it is possible that both the 32-bit and 64-bit hooks will be called if a 32-bit hook is ahead of a 64-bit hook in the hook chain.

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. Libraries that no longer need a hook should remove its hook procedure.
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. Libraries that no longer need a hook should remove its hook procedure.

<b>Windows Store app development</b> If dwThreadId is zero, then window hook DLLs are not loaded in-process for the Windows Store app processes and the Windows Runtime broker process unless they are installed by either UIAccess processes (accessibility tools). The notification is delivered on the installer's thread for these hooks:
<b>Windows Store apps: </b> If dwThreadId is zero, then window hook DLLs are not loaded in-process for the Windows Store app processes and the Windows Runtime broker process unless they are installed by either UIAccess processes (accessibility tools). The notification is delivered on the installer's thread for these hooks:

<ul>
<li>WH_JOURNALPLAYBACK</li>
Expand All @@ -400,13 +403,18 @@ The global hooks are a shared resource, and installing one affects all applicati

</li>
</ul>
This behavior is similar to what happens when there is an architecture mismatch between the hook DLL and the target application process, for example, when the hook DLL is 32-bit and the application process 64-bit.
This behavior is similar to what happens when there is an architecture mismatch between the hook DLL and the target application process, for example, when the hook DLL is 32-bit and the application process 64-bit.


#### Examples

For an example, see [Installing and Releasing Hook Procedures](/windows/desktop/winmsg/using-hooks).

<div class="code"></div>




> [!NOTE]
> The winuser.h header defines SetWindowsHookEx as an alias that automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see [Conventions for Function Prototypes](/windows/win32/intl/conventions-for-function-prototypes).

Expand Down
Loading