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
75 #include "user_private.h"
76 #include "wine/server.h"
78 #include "wine/debug.h"
81 WINE_DEFAULT_DEBUG_CHANNEL(hook
);
82 WINE_DECLARE_DEBUG_CHANNEL(relay
);
84 static struct user_api_hook original_user_api
=
89 static struct user_api_hook hooked_user_api
;
90 struct user_api_hook
*user_api
= &original_user_api
;
98 BOOL prev_unicode
, next_unicode
;
99 WCHAR module
[MAX_PATH
];
102 #define WH_WINEVENT (WH_MAXHOOK+1)
104 static const char * const hook_names
[WH_WINEVENT
- WH_MINHOOK
+ 1] =
108 "WH_JOURNALPLAYBACK",
126 /***********************************************************************
127 * get_ll_hook_timeout
130 static UINT
get_ll_hook_timeout(void)
132 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
137 /***********************************************************************
140 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
142 static HHOOK
set_windows_hook( INT id
, HOOKPROC proc
, HINSTANCE inst
, DWORD tid
, BOOL unicode
)
145 WCHAR module
[MAX_PATH
];
150 SetLastError( ERROR_INVALID_FILTER_PROC
);
154 if (tid
) /* thread-local hook */
156 if (id
== WH_JOURNALRECORD
||
157 id
== WH_JOURNALPLAYBACK
||
158 id
== WH_KEYBOARD_LL
||
160 id
== WH_SYSMSGFILTER
)
162 /* these can only be global */
163 SetLastError( ERROR_INVALID_PARAMETER
);
167 else /* system-global hook */
169 if (id
== WH_KEYBOARD_LL
|| id
== WH_MOUSE_LL
) inst
= 0;
172 SetLastError( ERROR_HOOK_NEEDS_HMOD
);
177 if (inst
&& (!(len
= GetModuleFileNameW( inst
, module
, MAX_PATH
)) || len
>= MAX_PATH
))
179 SetLastError( ERROR_INVALID_PARAMETER
);
183 SERVER_START_REQ( set_hook
)
188 req
->event_min
= EVENT_MIN
;
189 req
->event_max
= EVENT_MAX
;
190 req
->flags
= WINEVENT_INCONTEXT
;
191 req
->unicode
= unicode
;
192 if (inst
) /* make proc relative to the module base */
194 req
->proc
= wine_server_client_ptr( (void *)((char *)proc
- (char *)inst
) );
195 wine_server_add_data( req
, module
, lstrlenW(module
) * sizeof(WCHAR
) );
197 else req
->proc
= wine_server_client_ptr( proc
);
199 if (!wine_server_call_err( req
))
201 handle
= wine_server_ptr_handle( reply
->handle
);
202 get_user_thread_info()->active_hooks
= reply
->active_hooks
;
207 TRACE( "%s %p %x -> %p\n", hook_names
[id
-WH_MINHOOK
], proc
, tid
, handle
);
212 /* Some apps pass a non-stdcall proc to SetWindowsHookExA,
213 * so we need a small assembly wrapper to call the proc.
215 extern LRESULT
HOOKPROC_wrapper( HOOKPROC proc
,
216 INT code
, WPARAM wParam
, LPARAM lParam
);
217 __ASM_GLOBAL_FUNC( HOOKPROC_wrapper
,
219 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
220 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
222 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
224 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
226 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
228 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
232 "movl 8(%ebp),%eax\n\t"
234 "leal -12(%ebp),%esp\n\t"
236 __ASM_CFI(".cfi_same_value %ebx\n\t")
238 __ASM_CFI(".cfi_same_value %esi\n\t")
240 __ASM_CFI(".cfi_same_value %edi\n\t")
242 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
243 __ASM_CFI(".cfi_same_value %ebp\n\t")
246 static inline LRESULT
HOOKPROC_wrapper( HOOKPROC proc
,
247 INT code
, WPARAM wParam
, LPARAM lParam
)
249 return proc( code
, wParam
, lParam
);
251 #endif /* __i386__ */
254 /***********************************************************************
257 static LRESULT
call_hook_AtoW( HOOKPROC proc
, INT id
, INT code
, WPARAM wparam
, LPARAM lparam
)
260 UNICODE_STRING usBuffer
;
261 if (id
!= WH_CBT
|| code
!= HCBT_CREATEWND
)
262 ret
= HOOKPROC_wrapper( proc
, code
, wparam
, lparam
);
265 CBT_CREATEWNDA
*cbtcwA
= (CBT_CREATEWNDA
*)lparam
;
266 CBT_CREATEWNDW cbtcwW
;
269 LPWSTR classW
= NULL
;
272 cbtcwW
.hwndInsertAfter
= cbtcwA
->hwndInsertAfter
;
273 csW
= *(CREATESTRUCTW
*)cbtcwA
->lpcs
;
275 if (!IS_INTRESOURCE(cbtcwA
->lpcs
->lpszName
))
277 RtlCreateUnicodeStringFromAsciiz(&usBuffer
,cbtcwA
->lpcs
->lpszName
);
278 csW
.lpszName
= nameW
= usBuffer
.Buffer
;
280 if (!IS_INTRESOURCE(cbtcwA
->lpcs
->lpszClass
))
282 RtlCreateUnicodeStringFromAsciiz(&usBuffer
,cbtcwA
->lpcs
->lpszClass
);
283 csW
.lpszClass
= classW
= usBuffer
.Buffer
;
285 ret
= HOOKPROC_wrapper( proc
, code
, wparam
, (LPARAM
)&cbtcwW
);
286 cbtcwA
->hwndInsertAfter
= cbtcwW
.hwndInsertAfter
;
287 HeapFree( GetProcessHeap(), 0, nameW
);
288 HeapFree( GetProcessHeap(), 0, classW
);
294 /***********************************************************************
297 static LRESULT
call_hook_WtoA( HOOKPROC proc
, INT id
, INT code
, WPARAM wparam
, LPARAM lparam
)
301 if (id
!= WH_CBT
|| code
!= HCBT_CREATEWND
)
302 ret
= HOOKPROC_wrapper( proc
, code
, wparam
, lparam
);
305 CBT_CREATEWNDW
*cbtcwW
= (CBT_CREATEWNDW
*)lparam
;
306 CBT_CREATEWNDA cbtcwA
;
313 cbtcwA
.hwndInsertAfter
= cbtcwW
->hwndInsertAfter
;
314 csA
= *(CREATESTRUCTA
*)cbtcwW
->lpcs
;
316 if (!IS_INTRESOURCE(cbtcwW
->lpcs
->lpszName
)) {
317 len
= WideCharToMultiByte( CP_ACP
, 0, cbtcwW
->lpcs
->lpszName
, -1, NULL
, 0, NULL
, NULL
);
318 nameA
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(CHAR
) );
319 WideCharToMultiByte( CP_ACP
, 0, cbtcwW
->lpcs
->lpszName
, -1, nameA
, len
, NULL
, NULL
);
320 csA
.lpszName
= nameA
;
323 if (!IS_INTRESOURCE(cbtcwW
->lpcs
->lpszClass
)) {
324 len
= WideCharToMultiByte( CP_ACP
, 0, cbtcwW
->lpcs
->lpszClass
, -1, NULL
, 0, NULL
, NULL
);
325 classA
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(CHAR
) );
326 WideCharToMultiByte( CP_ACP
, 0, cbtcwW
->lpcs
->lpszClass
, -1, classA
, len
, NULL
, NULL
);
327 csA
.lpszClass
= classA
;
330 ret
= HOOKPROC_wrapper( proc
, code
, wparam
, (LPARAM
)&cbtcwA
);
331 cbtcwW
->hwndInsertAfter
= cbtcwA
.hwndInsertAfter
;
332 HeapFree( GetProcessHeap(), 0, nameA
);
333 HeapFree( GetProcessHeap(), 0, classA
);
339 /***********************************************************************
342 static LRESULT
call_hook_proc( HOOKPROC proc
, INT id
, INT code
, WPARAM wparam
, LPARAM lparam
,
343 BOOL prev_unicode
, BOOL next_unicode
)
347 TRACE_(relay
)( "\1Call hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx)\n",
348 proc
, hook_names
[id
-WH_MINHOOK
], code
, wparam
, lparam
);
350 if (!prev_unicode
== !next_unicode
) ret
= proc( code
, wparam
, lparam
);
351 else if (prev_unicode
) ret
= call_hook_WtoA( proc
, id
, code
, wparam
, lparam
);
352 else ret
= call_hook_AtoW( proc
, id
, code
, wparam
, lparam
);
354 TRACE_(relay
)( "\1Ret hook proc %p (id=%s,code=%x,wp=%08lx,lp=%08lx) retval=%08lx\n",
355 proc
, hook_names
[id
-WH_MINHOOK
], code
, wparam
, lparam
, ret
);
361 /***********************************************************************
364 * Retrieve the hook procedure real value for a module-relative proc
366 void *get_hook_proc( void *proc
, const WCHAR
*module
, HMODULE
*free_module
)
370 GetModuleHandleExW( 0, module
, &mod
);
374 TRACE( "loading %s\n", debugstr_w(module
) );
375 /* FIXME: the library will never be freed */
376 if (!(mod
= LoadLibraryExW(module
, NULL
, LOAD_WITH_ALTERED_SEARCH_PATH
))) return NULL
;
378 return (char *)mod
+ (ULONG_PTR
)proc
;
381 /***********************************************************************
384 * Call hook either in current thread or send message to the destination
387 static LRESULT
call_hook( struct hook_info
*info
, INT code
, WPARAM wparam
, LPARAM lparam
)
393 struct hook_extra_info h_extra
;
394 h_extra
.handle
= info
->handle
;
395 h_extra
.lparam
= lparam
;
397 TRACE( "calling hook in thread %04x %s code %x wp %lx lp %lx\n",
398 info
->tid
, hook_names
[info
->id
-WH_MINHOOK
], code
, wparam
, lparam
);
403 MSG_SendInternalMessageTimeout( info
->pid
, info
->tid
, WM_WINE_KEYBOARD_LL_HOOK
,
404 wparam
, (LPARAM
)&h_extra
, SMTO_ABORTIFHUNG
,
405 get_ll_hook_timeout(), &ret
);
408 MSG_SendInternalMessageTimeout( info
->pid
, info
->tid
, WM_WINE_MOUSE_LL_HOOK
,
409 wparam
, (LPARAM
)&h_extra
, SMTO_ABORTIFHUNG
,
410 get_ll_hook_timeout(), &ret
);
413 ERR("Unknown hook id %d\n", info
->id
);
420 struct user_thread_info
*thread_info
= get_user_thread_info();
421 HMODULE free_module
= 0;
424 * Windows protects from stack overflow in recursive hook calls. Different Windows
425 * allow different depths.
427 if (thread_info
->hook_call_depth
>= 25)
429 WARN("Too many hooks called recursively, skipping call.\n");
433 TRACE( "calling hook %p %s code %x wp %lx lp %lx module %s\n",
434 info
->proc
, hook_names
[info
->id
-WH_MINHOOK
], code
, wparam
,
435 lparam
, debugstr_w(info
->module
) );
437 if (!info
->module
[0] ||
438 (info
->proc
= get_hook_proc( info
->proc
, info
->module
, &free_module
)) != NULL
)
440 HHOOK prev
= thread_info
->hook
;
441 BOOL prev_unicode
= thread_info
->hook_unicode
;
443 thread_info
->hook
= info
->handle
;
444 thread_info
->hook_unicode
= info
->next_unicode
;
445 thread_info
->hook_call_depth
++;
446 ret
= call_hook_proc( info
->proc
, info
->id
, code
, wparam
, lparam
,
447 info
->prev_unicode
, info
->next_unicode
);
448 thread_info
->hook
= prev
;
449 thread_info
->hook_unicode
= prev_unicode
;
450 thread_info
->hook_call_depth
--;
452 if (free_module
) FreeLibrary(free_module
);
456 if (info
->id
== WH_KEYBOARD_LL
|| info
->id
== WH_MOUSE_LL
)
457 InterlockedIncrement( &global_key_state_counter
); /* force refreshing the key state cache */
463 /***********************************************************************
466 static BOOL
HOOK_IsHooked( INT id
)
468 struct user_thread_info
*thread_info
= get_user_thread_info();
470 if (!thread_info
->active_hooks
) return TRUE
;
471 return (thread_info
->active_hooks
& (1 << (id
- WH_MINHOOK
))) != 0;
475 /***********************************************************************
478 LRESULT
HOOK_CallHooks( INT id
, INT code
, WPARAM wparam
, LPARAM lparam
, BOOL unicode
)
480 struct user_thread_info
*thread_info
= get_user_thread_info();
481 struct hook_info info
;
486 if (!HOOK_IsHooked( id
))
488 TRACE( "skipping hook %s mask %x\n", hook_names
[id
-WH_MINHOOK
], thread_info
->active_hooks
);
492 ZeroMemory( &info
, sizeof(info
) - sizeof(info
.module
) );
493 info
.prev_unicode
= unicode
;
496 SERVER_START_REQ( start_hook_chain
)
499 req
->event
= EVENT_MIN
;
500 wine_server_set_reply( req
, info
.module
, sizeof(info
.module
)-sizeof(WCHAR
) );
501 if (!wine_server_call( req
))
503 info
.module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
504 info
.handle
= wine_server_ptr_handle( reply
->handle
);
505 info
.pid
= reply
->pid
;
506 info
.tid
= reply
->tid
;
507 info
.proc
= wine_server_get_ptr( reply
->proc
);
508 info
.next_unicode
= reply
->unicode
;
509 thread_info
->active_hooks
= reply
->active_hooks
;
514 if (!info
.tid
&& !info
.proc
) return 0;
515 ret
= call_hook( &info
, code
, wparam
, lparam
);
517 SERVER_START_REQ( finish_hook_chain
)
520 wine_server_call( req
);
527 /***********************************************************************
528 * SetWindowsHookA (USER32.@)
530 HHOOK WINAPI
SetWindowsHookA( INT id
, HOOKPROC proc
)
532 return SetWindowsHookExA( id
, proc
, 0, GetCurrentThreadId() );
536 /***********************************************************************
537 * SetWindowsHookW (USER32.@)
539 HHOOK WINAPI
SetWindowsHookW( INT id
, HOOKPROC proc
)
541 return SetWindowsHookExW( id
, proc
, 0, GetCurrentThreadId() );
545 /***********************************************************************
546 * SetWindowsHookExA (USER32.@)
548 HHOOK WINAPI
SetWindowsHookExA( INT id
, HOOKPROC proc
, HINSTANCE inst
, DWORD tid
)
550 return set_windows_hook( id
, proc
, inst
, tid
, FALSE
);
553 /***********************************************************************
554 * SetWindowsHookExW (USER32.@)
556 HHOOK WINAPI
SetWindowsHookExW( INT id
, HOOKPROC proc
, HINSTANCE inst
, DWORD tid
)
558 return set_windows_hook( id
, proc
, inst
, tid
, TRUE
);
562 /***********************************************************************
563 * UnhookWindowsHook (USER32.@)
565 BOOL WINAPI
UnhookWindowsHook( INT id
, HOOKPROC proc
)
569 TRACE( "%s %p\n", hook_names
[id
-WH_MINHOOK
], proc
);
571 SERVER_START_REQ( remove_hook
)
575 req
->proc
= wine_server_client_ptr( proc
);
576 ret
= !wine_server_call_err( req
);
577 if (ret
) get_user_thread_info()->active_hooks
= reply
->active_hooks
;
580 if (!ret
&& GetLastError() == ERROR_INVALID_HANDLE
) SetLastError( ERROR_INVALID_HOOK_HANDLE
);
586 /***********************************************************************
587 * UnhookWindowsHookEx (USER32.@)
589 BOOL WINAPI
UnhookWindowsHookEx( HHOOK hhook
)
593 SERVER_START_REQ( remove_hook
)
595 req
->handle
= wine_server_user_handle( hhook
);
597 ret
= !wine_server_call_err( req
);
598 if (ret
) get_user_thread_info()->active_hooks
= reply
->active_hooks
;
601 if (!ret
&& GetLastError() == ERROR_INVALID_HANDLE
) SetLastError( ERROR_INVALID_HOOK_HANDLE
);
606 /***********************************************************************
607 * CallNextHookEx (USER32.@)
609 LRESULT WINAPI
CallNextHookEx( HHOOK hhook
, INT code
, WPARAM wparam
, LPARAM lparam
)
611 struct user_thread_info
*thread_info
= get_user_thread_info();
612 struct hook_info info
;
614 ZeroMemory( &info
, sizeof(info
) - sizeof(info
.module
) );
616 SERVER_START_REQ( get_hook_info
)
618 req
->handle
= wine_server_user_handle( thread_info
->hook
);
620 req
->event
= EVENT_MIN
;
621 wine_server_set_reply( req
, info
.module
, sizeof(info
.module
)-sizeof(WCHAR
) );
622 if (!wine_server_call_err( req
))
624 info
.module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
625 info
.handle
= wine_server_ptr_handle( reply
->handle
);
627 info
.pid
= reply
->pid
;
628 info
.tid
= reply
->tid
;
629 info
.proc
= wine_server_get_ptr( reply
->proc
);
630 info
.next_unicode
= reply
->unicode
;
635 info
.prev_unicode
= thread_info
->hook_unicode
;
636 return call_hook( &info
, code
, wparam
, lparam
);
640 LRESULT
call_current_hook( HHOOK hhook
, INT code
, WPARAM wparam
, LPARAM lparam
)
642 struct hook_info info
;
644 ZeroMemory( &info
, sizeof(info
) - sizeof(info
.module
) );
646 SERVER_START_REQ( get_hook_info
)
648 req
->handle
= wine_server_user_handle( hhook
);
650 req
->event
= EVENT_MIN
;
651 wine_server_set_reply( req
, info
.module
, sizeof(info
.module
)-sizeof(WCHAR
) );
652 if (!wine_server_call_err( req
))
654 info
.module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
655 info
.handle
= wine_server_ptr_handle( reply
->handle
);
657 info
.pid
= reply
->pid
;
658 info
.tid
= reply
->tid
;
659 info
.proc
= wine_server_get_ptr( reply
->proc
);
660 info
.next_unicode
= reply
->unicode
;
665 info
.prev_unicode
= TRUE
; /* assume Unicode for this function */
666 return call_hook( &info
, code
, wparam
, lparam
);
669 /***********************************************************************
670 * CallMsgFilterA (USER32.@)
672 BOOL WINAPI
CallMsgFilterA( LPMSG msg
, INT code
)
674 if (HOOK_CallHooks( WH_SYSMSGFILTER
, code
, 0, (LPARAM
)msg
, FALSE
)) return TRUE
;
675 return HOOK_CallHooks( WH_MSGFILTER
, code
, 0, (LPARAM
)msg
, FALSE
);
679 /***********************************************************************
680 * CallMsgFilterW (USER32.@)
682 BOOL WINAPI
CallMsgFilterW( LPMSG msg
, INT code
)
684 if (HOOK_CallHooks( WH_SYSMSGFILTER
, code
, 0, (LPARAM
)msg
, TRUE
)) return TRUE
;
685 return HOOK_CallHooks( WH_MSGFILTER
, code
, 0, (LPARAM
)msg
, TRUE
);
689 /***********************************************************************
690 * SetWinEventHook [USER32.@]
692 * Set up an event hook for a set of events.
695 * event_min [I] Lowest event handled by pfnProc
696 * event_max [I] Highest event handled by pfnProc
697 * inst [I] DLL containing pfnProc
698 * proc [I] Callback event hook function
699 * pid [I] Process to get events from, or 0 for all processes
700 * tid [I] Thread to get events from, or 0 for all threads
701 * flags [I] Flags indicating the status of pfnProc
704 * Success: A handle representing the hook.
705 * Failure: A NULL handle.
707 HWINEVENTHOOK WINAPI
SetWinEventHook(DWORD event_min
, DWORD event_max
,
708 HMODULE inst
, WINEVENTPROC proc
,
709 DWORD pid
, DWORD tid
, DWORD flags
)
711 HWINEVENTHOOK handle
= 0;
712 WCHAR module
[MAX_PATH
];
715 TRACE("%d,%d,%p,%p,%08x,%04x,%08x\n", event_min
, event_max
, inst
,
716 proc
, pid
, tid
, flags
);
720 if (!(len
= GetModuleFileNameW(inst
, module
, MAX_PATH
)) || len
>= MAX_PATH
)
724 if ((flags
& WINEVENT_INCONTEXT
) && !inst
)
726 SetLastError(ERROR_HOOK_NEEDS_HMOD
);
730 if (event_min
> event_max
)
732 SetLastError(ERROR_INVALID_HOOK_FILTER
);
736 /* FIXME: what if the tid or pid belongs to another process? */
737 if (tid
) /* thread-local hook */
740 SERVER_START_REQ( set_hook
)
742 req
->id
= WH_WINEVENT
;
745 req
->event_min
= event_min
;
746 req
->event_max
= event_max
;
749 if (inst
) /* make proc relative to the module base */
751 req
->proc
= wine_server_client_ptr( (void *)((char *)proc
- (char *)inst
) );
752 wine_server_add_data( req
, module
, lstrlenW(module
) * sizeof(WCHAR
) );
754 else req
->proc
= wine_server_client_ptr( proc
);
756 if (!wine_server_call_err( req
))
758 handle
= wine_server_ptr_handle( reply
->handle
);
759 get_user_thread_info()->active_hooks
= reply
->active_hooks
;
764 TRACE("-> %p\n", handle
);
769 /***********************************************************************
770 * UnhookWinEvent [USER32.@]
772 * Remove an event hook for a set of events.
775 * hEventHook [I] Event hook to remove
778 * Success: TRUE. The event hook has been removed.
779 * Failure: FALSE, if hEventHook is invalid.
781 BOOL WINAPI
UnhookWinEvent(HWINEVENTHOOK hEventHook
)
785 SERVER_START_REQ( remove_hook
)
787 req
->handle
= wine_server_user_handle( hEventHook
);
788 req
->id
= WH_WINEVENT
;
789 ret
= !wine_server_call_err( req
);
790 if (ret
) get_user_thread_info()->active_hooks
= reply
->active_hooks
;
796 static inline BOOL
find_first_hook(DWORD id
, DWORD event
, HWND hwnd
, LONG object_id
,
797 LONG child_id
, struct hook_info
*info
)
799 struct user_thread_info
*thread_info
= get_user_thread_info();
802 if (!HOOK_IsHooked( id
))
804 TRACE( "skipping hook %s mask %x\n", hook_names
[id
-WH_MINHOOK
], thread_info
->active_hooks
);
808 SERVER_START_REQ( start_hook_chain
)
812 req
->window
= wine_server_user_handle( hwnd
);
813 req
->object_id
= object_id
;
814 req
->child_id
= child_id
;
815 wine_server_set_reply( req
, info
->module
, sizeof(info
->module
)-sizeof(WCHAR
) );
816 ret
= !wine_server_call( req
);
819 info
->module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
820 info
->handle
= wine_server_ptr_handle( reply
->handle
);
821 info
->proc
= wine_server_get_ptr( reply
->proc
);
822 info
->tid
= reply
->tid
;
823 thread_info
->active_hooks
= reply
->active_hooks
;
827 return ret
&& (info
->tid
|| info
->proc
);
830 static inline BOOL
find_next_hook(DWORD event
, HWND hwnd
, LONG object_id
,
831 LONG child_id
, struct hook_info
*info
)
835 SERVER_START_REQ( get_hook_info
)
837 req
->handle
= wine_server_user_handle( info
->handle
);
840 req
->window
= wine_server_user_handle( hwnd
);
841 req
->object_id
= object_id
;
842 req
->child_id
= child_id
;
843 wine_server_set_reply( req
, info
->module
, sizeof(info
->module
)-sizeof(WCHAR
) );
844 ret
= !wine_server_call( req
);
847 info
->module
[wine_server_reply_size(req
) / sizeof(WCHAR
)] = 0;
848 info
->handle
= wine_server_ptr_handle( reply
->handle
);
849 info
->proc
= wine_server_get_ptr( reply
->proc
);
850 info
->tid
= reply
->tid
;
857 static inline void find_hook_close(DWORD id
)
859 SERVER_START_REQ( finish_hook_chain
)
862 wine_server_call( req
);
867 /***********************************************************************
868 * NotifyWinEvent [USER32.@]
870 * Inform the OS that an event has occurred.
873 * event [I] Id of the event
874 * hwnd [I] Window holding the object that created the event
875 * object_id [I] Type of object that created the event
876 * child_id [I] Child object of nId, or CHILDID_SELF.
881 void WINAPI
NotifyWinEvent(DWORD event
, HWND hwnd
, LONG object_id
, LONG child_id
)
883 struct hook_info info
;
885 TRACE("%04x,%p,%d,%d\n", event
, hwnd
, object_id
, child_id
);
889 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
896 if (event
& 0x80000000)
898 /* FIXME: on 64-bit platforms we need to invent some other way for
899 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
900 * struct call_hook *hook = (LRESULT *)hWnd;
901 * wparam = hook->wparam;
902 * lparam = hook->lparam;
904 LRESULT
*ret
= (LRESULT
*)hwnd
;
905 INT id
, code
, unicode
;
907 id
= (dwEvent
& 0x7fff0000) >> 16;
908 code
= event
& 0x7fff;
909 unicode
= event
& 0x8000;
910 *ret
= HOOK_CallHooks(id
, code
, object_id
, child_id
, unicode
);
915 if (!find_first_hook(WH_WINEVENT
, event
, hwnd
, object_id
, child_id
, &info
)) return;
919 WINEVENTPROC proc
= info
.proc
;
922 HMODULE free_module
= 0;
923 TRACE( "calling WH_WINEVENT hook %p event %x hwnd %p %x %x module %s\n",
924 proc
, event
, hwnd
, object_id
, child_id
, debugstr_w(info
.module
) );
926 if (!info
.module
[0] || (proc
= get_hook_proc( proc
, info
.module
, &free_module
)) != NULL
)
928 TRACE_(relay
)( "\1Call winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
929 proc
, info
.handle
, event
, hwnd
, object_id
,
930 child_id
, GetCurrentThreadId(), GetCurrentTime());
932 proc( info
.handle
, event
, hwnd
, object_id
, child_id
,
933 GetCurrentThreadId(), GetCurrentTime());
935 TRACE_(relay
)( "\1Ret winevent hook proc %p (hhook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%x,tid=%04x,time=%x)\n",
936 proc
, info
.handle
, event
, hwnd
, object_id
,
937 child_id
, GetCurrentThreadId(), GetCurrentTime());
939 if (free_module
) FreeLibrary(free_module
);
945 while (find_next_hook(event
, hwnd
, object_id
, child_id
, &info
));
947 find_hook_close(WH_WINEVENT
);
951 /***********************************************************************
952 * IsWinEventHookInstalled [USER32.@]
954 * Determine if an event hook is installed for an event.
957 * dwEvent [I] Id of the event
960 * TRUE, If there are any hooks installed for the event.
966 BOOL WINAPI
IsWinEventHookInstalled(DWORD dwEvent
)
968 /* FIXME: Needed by Office 2007 installer */
969 WARN("(%d)-stub!\n", dwEvent
);
973 /* Undocumented RegisterUserApiHook() */
974 BOOL WINAPI
RegisterUserApiHook(const struct user_api_hook
*new, struct user_api_hook
*old
)
980 hooked_user_api
= *new;
981 user_api
= &hooked_user_api
;
983 *old
= original_user_api
;
988 /* Undocumented UnregisterUserApiHook() */
989 void WINAPI
UnregisterUserApiHook(void)
991 InterlockedExchangePointer((void **)&user_api
, &original_user_api
);