3 out of 8 rated this helpful - Rate this topic

UnhookWindowsHookEx function

Applies to: desktop apps only

Removes a hook procedure installed in a hook chain by the SetWindowsHookEx function.

Syntax

BOOL WINAPI UnhookWindowsHookEx(
  __in  HHOOK hhk
);

Parameters

hhk [in]

Type: HHOOK

A handle to the hook to be removed. This parameter is a hook handle obtained by a previous call to SetWindowsHookEx.

Return value

Type:

Type: BOOL

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The hook procedure can be in the state of being called by another thread even after UnhookWindowsHookEx returns. If the hook procedure is not being called concurrently, the hook procedure is removed immediately before UnhookWindowsHookEx returns.

Examples

For an example, see Monitoring System Events.

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
Conceptual
Hooks

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
DLL isn't really mapped until an adequate event happens

Found a good description of why hooks do not attach and detach immediately after SetWindowsHookEx and UnhookWindowsHookEx:

"After a successful call to SetWindowsHookEx, the system maps the DLL into the address space of the hooked thread automatically, but not necessary immediately. Because windows hooks are all about messages, the DLL isn't really mapped until an adequate event happens. The same is true for unmapping the DLL after calling UnhookWindowsHookEx. The DLL isn't really unmapped until an adequate event happens" by Robert Kuster

This article helped me a lot — http://www.codeproject.com/KB/threads/winspy.aspx?display=Print

Return value when a thread is terminated
If you UnhookWindowsHookEx a thread because it is "signaled" (terminated), the function will return a zero return value (failure). But GetLastError will say ERROR_SUCCESS. Note: in this situation, you have to call SetLastError( ERROR_SUCCESS) before UnhookWindowsHookEx if you target W2K (and if you want to check the success of the call)
C# syntax
  
[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
Visual Basic 9 declaration
 Public Declare Auto Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean