2 * Windows hook functions
4 * Copyright 2002 Alexandre Julliard
5 * Copyright 2005 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Status of the various hooks:
24 * WH_JOURNALRECORD Partially implemented
25 * WH_JOURNALPLAYBACK Partially implemented
27 * WH_GETMESSAGE OK (FIXME: A/W mapping?)
28 * WH_CALLWNDPROC OK (FIXME: A/W mapping?)
36 * HCBT_CLICKSKIPPED OK
42 * WH_HARDWARE Not supported in Win32
43 * WH_DEBUG Not implemented
45 * HSHELL_WINDOWCREATED OK
46 * HSHELL_WINDOWDESTROYED OK
47 * HSHELL_ACTIVATESHELLWINDOW Not implemented
48 * HSHELL_WINDOWACTIVATED Not implemented
49 * HSHELL_GETMINRECT Not implemented
50 * HSHELL_REDRAW Not implemented
51 * HSHELL_TASKMAN Not implemented
52 * HSHELL_LANGUAGE Not implemented
53 * HSHELL_SYSMENU Not implemented
54 * HSHELL_ENDTASK Not implemented
55 * HSHELL_ACCESSIBILITYSTATE Not implemented
56 * HSHELL_APPCOMMAND Not implemented
57 * HSHELL_WINDOWREPLACED Not implemented
58 * HSHELL_WINDOWREPLACING Not implemented
59 * WH_FOREGROUNDIDLE Not implemented
60 * WH_CALLWNDPROCRET OK (FIXME: A/W mapping?)
61 * WH_KEYBOARD_LL Implemented but should use SendMessage instead
62 * WH_MOUSE_LL Implemented but should use SendMessage instead
66 #include "wine/port.h"
77 #include "user_private.h"
78 #include "wine/server.h"
79 #include "wine/unicode.h"
80 #include "wine/debug.h"
83 WINE_DEFAULT_DEBUG_CHANNEL(hook
);
84 WINE_DECLARE_DEBUG_CHANNEL(relay
);
92 BOOL prev_unicode
, next_unicode
;
93 WCHAR module
[MAX_PATH
];
96 #define WH_WINEVENT (WH_MAXHOOK+1)
98 static const char * const hook_names
[WH_WINEVENT
- WH_MINHOOK
+ 1] =
102 "WH_JOURNALPLAYBACK",
120 /***********************************************************************
121 * get_ll_hook_timeout
124 static UINT
get_ll_hook_timeout(void)
126 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
131 /***********************************************************************
134 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
136 static HHOOK
set_windows_hook( INT id
, HOOKPROC proc
, HINSTANCE inst
, DWORD tid
, BOOL unicode
)
139 WCHAR module
[MAX_PATH
];
144 SetLastError( ERROR_INVALID_FILTER_PROC
);
148 if (tid
) /* thread-local hook */
150 if (id
== WH_JOURNALRECORD
||
151 id
== WH_JOURNALPLAYBACK
||
152 id
== WH_KEYBOARD_LL
||
154 id
== WH_SYSMSGFILTER
)
156 /* these can only be global */
157 SetLastError( ERROR_INVALID_PARAMETER
);
161 else /* system-global hook */
163 if (id
== WH_KEYBOARD_LL
|| id
== WH_MOUSE_LL
) inst
= 0;
166 SetLastError( ERROR_HOOK_NEEDS_HMOD
);
171 if (inst
&& (!(len
= GetModuleFileNameW( inst
, module
, MAX_PATH
)) || len
>= MAX_PATH
))
173 SetLastError( ERROR_INVALID_PARAMETER
);
177 SERVER_START_REQ( set_hook
)
182 req
->event_min
= EVENT_MIN
;
183 req
->event_max
= EVENT_MAX
;
184 req
->flags
= WINEVENT_INCONTEXT
;
185 req
->unicode
= unicode
;
186 if (inst
) /* make proc relative to the module base */
188 req
->proc
= wine_server_client_ptr( (void *)((char *)proc
- (char *)inst
) );
189 wine_server_add_data( req
, module
, strlenW(module
) * sizeof(WCHAR
) );
191 else req
->proc
= wine_server_client_ptr( proc
);
193 if (!wine_server_call_err( req
))
195 handle
= wine_server_ptr_handle( reply
->handle
);
196 get_user_thread_info()->active_hooks
= reply
->active_hooks
;
201 TRACE( "%s %p %x -> %p\n", hook_names
[id
-WH_MINHOOK
], proc
, tid
, handle
);
206 /* Some apps pass a non-stdcall proc to SetWindowsHookExA,
207 * so we need a small assembly wrapper to call the proc.
209 extern LRESULT
HOOKPROC_wrapper( HOOKPROC proc
,
210 INT code
, WPARAM wParam
, LPARAM lParam
);
211 __ASM_GLOBAL_FUNC( HOOKPROC_wrapper
,
213 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
214 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
216 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
218 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
220 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
222 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
226 "movl 8(%ebp),%eax\n\t"
228 "leal -12(%ebp),%esp\n\t"
230 __ASM_CFI(".cfi_same_value %ebx\n\t")
232 __ASM_CFI(".cfi_same_value %esi\n\t")
234 __ASM_CFI(".cfi_same_value %edi\n\t")
236 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
237 __ASM_CFI(".cfi_same_value %ebp\n\t")
240 static inline LRESULT
HOOKPROC_wrapper( HOOKPROC proc
,
241 INT code
, WPARAM wParam
, LPARAM lParam
)
243 return proc( code
, wParam
, lParam
);
245 #endif /* __i386__ */
248 /***********************************************************************
251 static LRESULT
call_hook_AtoW( HOOKPROC proc
, INT id
, INT code
, WPARAM wparam
, LPARAM lparam
)
254 UNICODE_STRING usBuffer
;
255 if (id
!= WH_CBT
|| code
!= HCBT_CREATEWND
)
256 ret
= HOOKPROC_wrapper( proc
, code
, wparam
, lparam
);
259 CBT_CREATEWNDA
*cbtcwA
= (CBT_CREATEWNDA
*)lparam
;
260 CBT_CREATEWNDW cbtcwW
;
263 LPWSTR classW
= NULL
;
266 cbtcwW
.hwndInsertAfter
= cbtcwA
->hwndInsertAfter
;
267 csW
= *(CREATESTRUCTW
*)cbtcwA
->lpcs
;
269 if (!IS_INTRESOURCE(cbtcwA
->lpcs
->lpszName
))
271 RtlCreateUnicodeStringFromAsciiz(&usBuffer
,cbtcwA
->lpcs
->lpszName
);
272 csW
.lpszName
= nameW
= usBuffer
.Buffer
;
274 if (!IS_INTRESOURCE(cbtcwA
->lpcs
->lpszClass
))
276 RtlCreateUnicodeStringFromAsciiz(&usBuffer
,cbtcwA
->lpcs
->lpszClass
);
277 csW
.lpszClass
= classW
= usBuffer
.Buffer
;
279 ret
= HOOKPROC_wrapper( proc
, code
, wparam
, (LPARAM
)&cbtcwW
);
280 cbtcwA
->hwndInsertAfter
= cbtcwW
.hwndInsertAfter
;
281 HeapFree( GetProcessHeap(), 0, nameW
);
282 HeapFree( GetProcessHeap(), 0, classW
);
288 /***********************************************************************
291 static LRESULT
call_hook_WtoA( HOOKPROC proc
, INT id
, INT code
, WPARAM wparam
, LPARAM lparam
)
295 if (id
!= WH_CBT
|| code
!= HCBT_CREATEWND
)
296 ret
= HOOKPROC_wrapper( proc
, code
, wparam
, lparam
);
299 CBT_CREATEWNDW
*cbtcwW
= (CBT_CREATEWNDW
*)lparam
;
300 CBT_CREATEWNDA cbtcwA
;
307 cbtcwA
.hwndInsertAfter
= cbtcwW
->hwndInsertAfter
;
308 csA
= *(CREATESTRUCTA
*)cbtcwW
->lpcs
;
310 if (!IS_INTRESOURCE(cbtcwW
->lpcs
->lpszName
)) {
311 len
= WideCharToMultiByte( CP_ACP
, 0, cbtcwW
->lpcs
->lpszName
, -1, NULL
, 0, NULL
, NULL
);
312 nameA
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(CHAR
) );
313 WideCharToMultiByte( CP_ACP
, 0, cbtcwW
->lpcs
->lpszName
, -1, nameA
, len
, NULL
, NULL
);
314 csA
.lpszName
= nameA
;
317 if (!IS_INTRESOURCE(cbtcwW
->lpcs
->lpszClass
)) {
318 len
= WideCharToMultiByte( CP_ACP
, 0, cbtcwW
->lpcs
->lpszClass
, -1, NULL
, 0, NULL
, NULL
);
319 classA
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(CHAR
) );
320 WideCharToMultiByte( CP_ACP
, 0, cbtcwW
->lpcs
->lpszClass
, -1, classA
, len
, NULL
, NULL
);
321 csA
.lpszClass
= classA
;
324 ret
= HOOKPROC_wrapper( proc
, code
, wparam
, (LPARAM
)&cbtcwA
);
325 cbtcwW
->hwndInsertAfter
= cbtcwA
.hwndInsertAfter
;
326 HeapFree( GetProcessHeap(), 0, nameA
);
327 HeapFree( GetProcessHeap(), 0, classA
);
333 /***********************************************************************
336 static LRESULT
call_hook_proc( HOOKPROC proc
, INT id
, INT code
, WPARAM wparam
, LPARAM lparam
,
337 BOOL prev_unicode
, BOOL next_unicode
)
341 TRACE_(relay
)( "\1Call hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx)\n",
342 proc
, hook_names
[id
-WH_MINHOOK
], code
, wparam
, lparam
);
344 if (!prev_unicode
== !next_unicode
) ret
= proc( code
, wparam
, lparam
);
345 else if (prev_unicode
) ret
= call_hook_WtoA( proc
, id
, code
, wparam
, lparam
);
346 else ret
= call_hook_AtoW( proc
, id
, code
, wparam
, lparam
);
348 TRACE_(relay
)( "\1Ret hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx) retval=%08lx\n",
349 proc
, hook_names
[id
-WH_MINHOOK
], code
, wparam
, lparam
, ret
);
355 /***********************************************************************
358 * Retrieve the hook procedure real value for a module-relative proc
360 void *get_hook_proc( void *proc
, const WCHAR
*module
, HMODULE
*free_module
)
364 GetModuleHandleExW( 0, module
, &mod
);
368 TRACE( "loading %s\n", debugstr_w(module
) );
369 /* FIXME: the library will never be freed */
370 if (!(mod
= LoadLibraryExW(module
, NULL
, LOAD_WITH_ALTERED_SEARCH_PATH
))) return NULL
;
372 return (char *)mod
+ (ULONG_PTR
)proc
;
375 /***********************************************************************
378 * Call hook either in current thread or send message to the destination
381 static LRESULT
call_hook( struct hook_info
*info
, INT code
, WPARAM wparam
, LPARAM lparam
)
387 struct hook_extra_info h_extra
;
388 h_extra
.handle
= info
->handle
;
389 h_extra
.lparam
= lparam
;
391 TRACE( "calling hook in thread %04x %s code %x wp %lx lp %lx\n",
392 info
->tid
, hook_names
[info
->id
-WH_MINHOOK
], code
, wparam
, lparam
);
397 MSG_SendInternalMessageTimeout( info
->pid
, info
->tid
, WM_WINE_KEYBOARD_LL_HOOK
,
398 wparam
, (LPARAM
)&h_extra
, SMTO_ABORTIFHUNG
,
399 get_ll_hook_timeout(), &ret
);
402 MSG_SendInternalMessageTimeout( info
->pid
, info
->tid
, WM_WINE_MOUSE_LL_HOOK
,
403 wparam
, (LPARAM
)&h_extra
, SMTO_ABORTIFHUNG
,
404 get_ll_hook_timeout(), &ret
);
407 ERR("Unknown hook id %d\n", info
->id
);
414 struct user_thread_info
*thread_info
= get_user_thread_info();
415 HMODULE free_module
= 0;
418 * Windows protects from stack overflow in recursive hook calls. Different Windows
419 * allow different depths.
421 if (thread_info
->hook_call_depth
>= 25)
423 WARN("Too many hooks called recursively, skipping call.\n");
427 TRACE( "calling hook %p %s code %x wp %lx lp %lx module %s\n",
428 info
->proc
, hook_names
[info
->id
-WH_MINHOOK
], code
, wparam
,
429 lparam
, debugstr_w(info
->module
) );
431 if (!info
->module
[0] ||
432 (info
->proc
= get_hook_proc( info
->proc
, info
->module
, &free_module
)) != NULL
)
434 HHOOK prev
= thread_info
->hook
;
435 BOOL prev_unicode
= thread_info
->hook_unicode
;
437 thread_info
->hook
= info
->handle
;
438 thread_info
->hook_unicode
= info
->next_unicode
;
439 thread_info
->hook_call_depth
++;
440 ret
= call_hook_proc( info
->proc
, info
->id
, code
, wparam
, lparam
,
441 info
->prev_unicode
, info
->next_unicode
);
442 thread_info
->hook
= prev
;
443 thread_info
->hook_unicode
= prev_unicode
;
444 thread_info
->hook_call_depth
--;
446 if (free_module
) FreeLibrary(free_module
);
450 if (info
->id
== WH_KEYBOARD_LL
|| info
->id
== WH_MOUSE_LL
)
451 interlocked_xchg_add( &global_key_state_counter
, 1 ); /* force refreshing the key state cache */
457 /***********************************************************************
460 static BOOL
HOOK_IsHooked( INT id
)
462 struct user_thread_info
*thread_info
= get_user_thread_info();
464 if (!thread_info
->active_hooks
) return TRUE
;
465 return (thread_info
->active_hooks
& (1 << (id
- WH_MINHOOK
))) != 0;
469 /***********************************************************************
472 LRESULT
HOOK_CallHooks( INT id
, INT code
, WPARAM wparam
, LPARAM lparam
, BOOL unicode
)
474 struct user_thread_info
*thread_info
= get_user_thread_info();
475 struct hook_info info
;
480 if (!HOOK_IsHooked( id
))
482 TRACE( "skipping hook %s mask %x\n", hook_names
[id
-WH_MINHOOK
], thread_info
->active_hooks
);
486 ZeroMemory( &info
, sizeof(info
) - sizeof(info
.module
) );
487 info
.prev_unicode
= unicode
;
490 SERVER_START_REQ( start_hook_chain
)
493 req
->event
= EVENT_MIN
;
494 wine_server_set_reply( req
, info
.module
, sizeof(info
.module
)-sizeof(WCHAR
) );
495 if (!wine_server_call( req
))
497 info
.module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
498 info
.handle
= wine_server_ptr_handle( reply
->handle
);
499 info
.pid
= reply
->pid
;
500 info
.tid
= reply
->tid
;
501 info
.proc
= wine_server_get_ptr( reply
->proc
);
502 info
.next_unicode
= reply
->unicode
;
503 thread_info
->active_hooks
= reply
->active_hooks
;
508 if (!info
.tid
&& !info
.proc
) return 0;
509 ret
= call_hook( &info
, code
, wparam
, lparam
);
511 SERVER_START_REQ( finish_hook_chain
)
514 wine_server_call( req
);
521 /***********************************************************************
522 * SetWindowsHookA (USER32.@)
524 HHOOK WINAPI
SetWindowsHookA( INT id
, HOOKPROC proc
)
526 return SetWindowsHookExA( id
, proc
, 0, GetCurrentThreadId() );
530 /***********************************************************************
531 * SetWindowsHookW (USER32.@)
533 HHOOK WINAPI
SetWindowsHookW( INT id
, HOOKPROC proc
)
535 return SetWindowsHookExW( id
, proc
, 0, GetCurrentThreadId() );
539 /***********************************************************************
540 * SetWindowsHookExA (USER32.@)
542 HHOOK WINAPI
SetWindowsHookExA( INT id
, HOOKPROC proc
, HINSTANCE inst
, DWORD tid
)
544 return set_windows_hook( id
, proc
, inst
, tid
, FALSE
);
547 /***********************************************************************
548 * SetWindowsHookExW (USER32.@)
550 HHOOK WINAPI
SetWindowsHookExW( INT id
, HOOKPROC proc
, HINSTANCE inst
, DWORD tid
)
552 return set_windows_hook( id
, proc
, inst
, tid
, TRUE
);
556 /***********************************************************************
557 * UnhookWindowsHook (USER32.@)
559 BOOL WINAPI
UnhookWindowsHook( INT id
, HOOKPROC proc
)
563 TRACE( "%s %p\n", hook_names
[id
-WH_MINHOOK
], proc
);
565 SERVER_START_REQ( remove_hook
)
569 req
->proc
= wine_server_client_ptr( proc
);
570 ret
= !wine_server_call_err( req
);
571 if (ret
) get_user_thread_info()->active_hooks
= reply
->active_hooks
;
574 if (!ret
&& GetLastError() == ERROR_INVALID_HANDLE
) SetLastError( ERROR_INVALID_HOOK_HANDLE
);
580 /***********************************************************************
581 * UnhookWindowsHookEx (USER32.@)
583 BOOL WINAPI
UnhookWindowsHookEx( HHOOK hhook
)
587 SERVER_START_REQ( remove_hook
)
589 req
->handle
= wine_server_user_handle( hhook
);
591 ret
= !wine_server_call_err( req
);
592 if (ret
) get_user_thread_info()->active_hooks
= reply
->active_hooks
;
595 if (!ret
&& GetLastError() == ERROR_INVALID_HANDLE
) SetLastError( ERROR_INVALID_HOOK_HANDLE
);
600 /***********************************************************************
601 * CallNextHookEx (USER32.@)
603 LRESULT WINAPI
CallNextHookEx( HHOOK hhook
, INT code
, WPARAM wparam
, LPARAM lparam
)
605 struct user_thread_info
*thread_info
= get_user_thread_info();
606 struct hook_info info
;
608 ZeroMemory( &info
, sizeof(info
) - sizeof(info
.module
) );
610 SERVER_START_REQ( get_hook_info
)
612 req
->handle
= wine_server_user_handle( thread_info
->hook
);
614 req
->event
= EVENT_MIN
;
615 wine_server_set_reply( req
, info
.module
, sizeof(info
.module
)-sizeof(WCHAR
) );
616 if (!wine_server_call_err( req
))
618 info
.module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
619 info
.handle
= wine_server_ptr_handle( reply
->handle
);
621 info
.pid
= reply
->pid
;
622 info
.tid
= reply
->tid
;
623 info
.proc
= wine_server_get_ptr( reply
->proc
);
624 info
.next_unicode
= reply
->unicode
;
629 info
.prev_unicode
= thread_info
->hook_unicode
;
630 return call_hook( &info
, code
, wparam
, lparam
);
634 LRESULT
call_current_hook( HHOOK hhook
, INT code
, WPARAM wparam
, LPARAM lparam
)
636 struct hook_info info
;
638 ZeroMemory( &info
, sizeof(info
) - sizeof(info
.module
) );
640 SERVER_START_REQ( get_hook_info
)
642 req
->handle
= wine_server_user_handle( hhook
);
644 req
->event
= EVENT_MIN
;
645 wine_server_set_reply( req
, info
.module
, sizeof(info
.module
)-sizeof(WCHAR
) );
646 if (!wine_server_call_err( req
))
648 info
.module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
649 info
.handle
= wine_server_ptr_handle( reply
->handle
);
651 info
.pid
= reply
->pid
;
652 info
.tid
= reply
->tid
;
653 info
.proc
= wine_server_get_ptr( reply
->proc
);
654 info
.next_unicode
= reply
->unicode
;
659 info
.prev_unicode
= TRUE
; /* assume Unicode for this function */
660 return call_hook( &info
, code
, wparam
, lparam
);
663 /***********************************************************************
664 * CallMsgFilterA (USER32.@)
666 BOOL WINAPI
CallMsgFilterA( LPMSG msg
, INT code
)
668 if (HOOK_CallHooks( WH_SYSMSGFILTER
, code
, 0, (LPARAM
)msg
, FALSE
)) return TRUE
;
669 return HOOK_CallHooks( WH_MSGFILTER
, code
, 0, (LPARAM
)msg
, FALSE
);
673 /***********************************************************************
674 * CallMsgFilterW (USER32.@)
676 BOOL WINAPI
CallMsgFilterW( LPMSG msg
, INT code
)
678 if (HOOK_CallHooks( WH_SYSMSGFILTER
, code
, 0, (LPARAM
)msg
, TRUE
)) return TRUE
;
679 return HOOK_CallHooks( WH_MSGFILTER
, code
, 0, (LPARAM
)msg
, TRUE
);
683 /***********************************************************************
684 * SetWinEventHook [USER32.@]
686 * Set up an event hook for a set of events.
689 * event_min [I] Lowest event handled by pfnProc
690 * event_max [I] Highest event handled by pfnProc
691 * inst [I] DLL containing pfnProc
692 * proc [I] Callback event hook function
693 * pid [I] Process to get events from, or 0 for all processes
694 * tid [I] Thread to get events from, or 0 for all threads
695 * flags [I] Flags indicating the status of pfnProc
698 * Success: A handle representing the hook.
699 * Failure: A NULL handle.
701 HWINEVENTHOOK WINAPI
SetWinEventHook(DWORD event_min
, DWORD event_max
,
702 HMODULE inst
, WINEVENTPROC proc
,
703 DWORD pid
, DWORD tid
, DWORD flags
)
705 HWINEVENTHOOK handle
= 0;
706 WCHAR module
[MAX_PATH
];
709 TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min
, event_max
, inst
,
710 proc
, pid
, tid
, flags
);
714 if (!(len
= GetModuleFileNameW(inst
, module
, MAX_PATH
)) || len
>= MAX_PATH
)
718 if ((flags
& WINEVENT_INCONTEXT
) && !inst
)
720 SetLastError(ERROR_HOOK_NEEDS_HMOD
);
724 if (event_min
> event_max
)
726 SetLastError(ERROR_INVALID_HOOK_FILTER
);
730 /* FIXME: what if the tid or pid belongs to another process? */
731 if (tid
) /* thread-local hook */
734 SERVER_START_REQ( set_hook
)
736 req
->id
= WH_WINEVENT
;
739 req
->event_min
= event_min
;
740 req
->event_max
= event_max
;
743 if (inst
) /* make proc relative to the module base */
745 req
->proc
= wine_server_client_ptr( (void *)((char *)proc
- (char *)inst
) );
746 wine_server_add_data( req
, module
, strlenW(module
) * sizeof(WCHAR
) );
748 else req
->proc
= wine_server_client_ptr( proc
);
750 if (!wine_server_call_err( req
))
752 handle
= wine_server_ptr_handle( reply
->handle
);
753 get_user_thread_info()->active_hooks
= reply
->active_hooks
;
758 TRACE("-> %p\n", handle
);
763 /***********************************************************************
764 * UnhookWinEvent [USER32.@]
766 * Remove an event hook for a set of events.
769 * hEventHook [I] Event hook to remove
772 * Success: TRUE. The event hook has been removed.
773 * Failure: FALSE, if hEventHook is invalid.
775 BOOL WINAPI
UnhookWinEvent(HWINEVENTHOOK hEventHook
)
779 SERVER_START_REQ( remove_hook
)
781 req
->handle
= wine_server_user_handle( hEventHook
);
782 req
->id
= WH_WINEVENT
;
783 ret
= !wine_server_call_err( req
);
784 if (ret
) get_user_thread_info()->active_hooks
= reply
->active_hooks
;
790 static inline BOOL
find_first_hook(DWORD id
, DWORD event
, HWND hwnd
, LONG object_id
,
791 LONG child_id
, struct hook_info
*info
)
793 struct user_thread_info
*thread_info
= get_user_thread_info();
796 if (!HOOK_IsHooked( id
))
798 TRACE( "skipping hook %s mask %x\n", hook_names
[id
-WH_MINHOOK
], thread_info
->active_hooks
);
802 SERVER_START_REQ( start_hook_chain
)
806 req
->window
= wine_server_user_handle( hwnd
);
807 req
->object_id
= object_id
;
808 req
->child_id
= child_id
;
809 wine_server_set_reply( req
, info
->module
, sizeof(info
->module
)-sizeof(WCHAR
) );
810 ret
= !wine_server_call( req
);
813 info
->module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
814 info
->handle
= wine_server_ptr_handle( reply
->handle
);
815 info
->proc
= wine_server_get_ptr( reply
->proc
);
816 info
->tid
= reply
->tid
;
817 thread_info
->active_hooks
= reply
->active_hooks
;
821 return ret
&& (info
->tid
|| info
->proc
);
824 static inline BOOL
find_next_hook(DWORD event
, HWND hwnd
, LONG object_id
,
825 LONG child_id
, struct hook_info
*info
)
829 SERVER_START_REQ( get_hook_info
)
831 req
->handle
= wine_server_user_handle( info
->handle
);
834 req
->window
= wine_server_user_handle( hwnd
);
835 req
->object_id
= object_id
;
836 req
->child_id
= child_id
;
837 wine_server_set_reply( req
, info
->module
, sizeof(info
->module
)-sizeof(WCHAR
) );
838 ret
= !wine_server_call( req
);
841 info
->module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
842 info
->handle
= wine_server_ptr_handle( reply
->handle
);
843 info
->proc
= wine_server_get_ptr( reply
->proc
);
844 info
->tid
= reply
->tid
;
851 static inline void find_hook_close(DWORD id
)
853 SERVER_START_REQ( finish_hook_chain
)
856 wine_server_call( req
);
861 /***********************************************************************
862 * NotifyWinEvent [USER32.@]
864 * Inform the OS that an event has occurred.
867 * event [I] Id of the event
868 * hwnd [I] Window holding the object that created the event
869 * object_id [I] Type of object that created the event
870 * child_id [I] Child object of nId, or CHILDID_SELF.
875 void WINAPI
NotifyWinEvent(DWORD event
, HWND hwnd
, LONG object_id
, LONG child_id
)
877 struct hook_info info
;
879 TRACE("%04x,%p,%d,%d\n", event
, hwnd
, object_id
, child_id
);
883 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
890 if (event
& 0x80000000)
892 /* FIXME: on 64-bit platforms we need to invent some other way for
893 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
894 * struct call_hook *hook = (LRESULT *)hWnd;
895 * wparam = hook->wparam;
896 * lparam = hook->lparam;
898 LRESULT
*ret
= (LRESULT
*)hwnd
;
899 INT id
, code
, unicode
;
901 id
= (dwEvent
& 0x7fff0000) >> 16;
902 code
= event
& 0x7fff;
903 unicode
= event
& 0x8000;
904 *ret
= HOOK_CallHooks(id
, code
, object_id
, child_id
, unicode
);
909 if (!find_first_hook(WH_WINEVENT
, event
, hwnd
, object_id
, child_id
, &info
)) return;
913 WINEVENTPROC proc
= info
.proc
;
916 HMODULE free_module
= 0;
917 TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
918 proc
, event
, hwnd
, object_id
, child_id
, debugstr_w(info
.module
) );
920 if (!info
.module
[0] || (proc
= get_hook_proc( proc
, info
.module
, &free_module
)) != NULL
)
922 TRACE_(relay
)( "\1Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
923 proc
, info
.handle
, event
, hwnd
, object_id
,
924 child_id
, GetCurrentThreadId(), GetCurrentTime());
926 proc( info
.handle
, event
, hwnd
, object_id
, child_id
,
927 GetCurrentThreadId(), GetCurrentTime());
929 TRACE_(relay
)( "\1Ret winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
930 proc
, info
.handle
, event
, hwnd
, object_id
,
931 child_id
, GetCurrentThreadId(), GetCurrentTime());
933 if (free_module
) FreeLibrary(free_module
);
939 while (find_next_hook(event
, hwnd
, object_id
, child_id
, &info
));
941 find_hook_close(WH_WINEVENT
);
945 /***********************************************************************
946 * IsWinEventHookInstalled [USER32.@]
948 * Determine if an event hook is installed for an event.
951 * dwEvent [I] Id of the event
954 * TRUE, If there are any hooks installed for the event.
960 BOOL WINAPI
IsWinEventHookInstalled(DWORD dwEvent
)
962 /* FIXME: Needed by Office 2007 installer */
963 WARN("(%d)-stub!\n", dwEvent
);