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"
76 #include "user_private.h"
77 #include "wine/server.h"
78 #include "wine/unicode.h"
79 #include "wine/debug.h"
82 WINE_DEFAULT_DEBUG_CHANNEL(hook);
83 WINE_DECLARE_DEBUG_CHANNEL(relay);
90 WCHAR module[MAX_PATH];
93 #define WH_WINEVENT (WH_MAXHOOK+1)
95 static const char * const hook_names[WH_WINEVENT - WH_MINHOOK + 1] =
117 /***********************************************************************
118 * get_ll_hook_timeout
121 static UINT get_ll_hook_timeout(void)
123 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
128 /***********************************************************************
131 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
133 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
136 WCHAR module[MAX_PATH];
141 SetLastError( ERROR_INVALID_FILTER_PROC );
145 /* FIXME: what if the tid belongs to another process? */
146 if (tid) /* thread-local hook */
148 if (id == WH_JOURNALRECORD ||
149 id == WH_JOURNALPLAYBACK ||
150 id == WH_KEYBOARD_LL ||
152 id == WH_SYSMSGFILTER)
154 /* these can only be global */
155 SetLastError( ERROR_INVALID_PARAMETER );
160 else /* system-global hook */
162 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
163 else if (!inst || !(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH)
165 SetLastError( ERROR_HOOK_NEEDS_HMOD );
170 SERVER_START_REQ( set_hook )
175 req->event_min = EVENT_MIN;
176 req->event_max = EVENT_MAX;
177 req->flags = WINEVENT_INCONTEXT;
178 req->unicode = unicode;
179 if (inst) /* make proc relative to the module base */
181 req->proc = (void *)((char *)proc - (char *)inst);
182 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
184 else req->proc = proc;
186 if (!wine_server_call_err( req ))
188 handle = reply->handle;
189 get_user_thread_info()->active_hooks = reply->active_hooks;
194 TRACE( "%s %p %lx -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
199 /***********************************************************************
202 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
205 UNICODE_STRING usBuffer;
206 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
209 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
210 CBT_CREATEWNDW cbtcwW;
214 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
215 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
217 if (HIWORD(cbtcwA->lpcs->lpszName))
219 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
220 csW.lpszName = usBuffer.Buffer;
222 if (HIWORD(cbtcwA->lpcs->lpszClass))
224 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
225 csW.lpszClass = usBuffer.Buffer;
227 ret = proc( code, wparam, (LPARAM)&cbtcwW );
228 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
229 if (HIWORD(csW.lpszName)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszName );
230 if (HIWORD(csW.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszClass );
236 /***********************************************************************
239 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
243 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
246 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
247 CBT_CREATEWNDA cbtcwA;
252 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
253 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
255 if (HIWORD(cbtcwW->lpcs->lpszName)) {
256 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, NULL, 0, NULL, NULL );
257 csA.lpszName = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
258 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, (LPSTR)csA.lpszName, len, NULL, NULL );
261 if (HIWORD(cbtcwW->lpcs->lpszClass)) {
262 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, NULL, 0, NULL, NULL );
263 csA.lpszClass = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
264 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, (LPSTR)csA.lpszClass, len, NULL, NULL );
267 ret = proc( code, wparam, (LPARAM)&cbtcwA );
268 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
269 if (HIWORD(csA.lpszName)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszName );
270 if (HIWORD(csA.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszClass );
276 /***********************************************************************
279 static LRESULT call_hook( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
280 BOOL prev_unicode, BOOL next_unicode )
285 DPRINTF( "%04lx:Call hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx)\n",
286 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
288 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
289 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
290 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
293 DPRINTF( "%04lx:Ret hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx) retval=%08lx\n",
294 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
300 /***********************************************************************
303 * Retrieve the hook procedure real value for a module-relative proc
305 static void *get_hook_proc( void *proc, const WCHAR *module )
309 if (!(mod = GetModuleHandleW(module)))
311 TRACE( "loading %s\n", debugstr_w(module) );
312 /* FIXME: the library will never be freed */
313 if (!(mod = LoadLibraryW(module))) return NULL;
315 return (char *)mod + (ULONG_PTR)proc;
319 /***********************************************************************
322 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
324 struct user_thread_info *thread_info = get_user_thread_info();
325 HOOKPROC proc = NULL;
327 DWORD pid = 0, tid = 0;
328 WCHAR module[MAX_PATH];
329 BOOL unicode_hook = FALSE;
334 if (!HOOK_IsHooked( id ))
336 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
340 SERVER_START_REQ( start_hook_chain )
343 req->event = EVENT_MIN;
344 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
345 if (!wine_server_call( req ))
347 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
348 handle = reply->handle;
352 unicode_hook = reply->unicode;
353 thread_info->active_hooks = reply->active_hooks;
360 TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
361 tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
366 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
367 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
370 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
371 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
374 ERR("Unknown hook id %d\n", id);
381 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
382 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
384 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
386 HHOOK prev = thread_info->hook;
387 thread_info->hook = handle;
388 ret = call_hook( proc, id, code, wparam, lparam, unicode, unicode_hook );
389 thread_info->hook = prev;
395 SERVER_START_REQ( finish_hook_chain )
398 wine_server_call( req );
405 /***********************************************************************
408 BOOL HOOK_IsHooked( INT id )
410 struct user_thread_info *thread_info = get_user_thread_info();
412 if (!thread_info->active_hooks) return TRUE;
413 return (thread_info->active_hooks & (1 << (id - WH_MINHOOK))) != 0;
417 /***********************************************************************
418 * SetWindowsHookA (USER32.@)
420 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
422 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
426 /***********************************************************************
427 * SetWindowsHookW (USER32.@)
429 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
431 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
435 /***********************************************************************
436 * SetWindowsHookExA (USER32.@)
438 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
440 return set_windows_hook( id, proc, inst, tid, FALSE );
443 /***********************************************************************
444 * SetWindowsHookExW (USER32.@)
446 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
448 return set_windows_hook( id, proc, inst, tid, TRUE );
452 /***********************************************************************
453 * UnhookWindowsHook (USER32.@)
455 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
459 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
461 SERVER_START_REQ( remove_hook )
466 ret = !wine_server_call_err( req );
467 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
470 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
476 /***********************************************************************
477 * UnhookWindowsHookEx (USER32.@)
479 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
483 TRACE( "%p\n", hhook );
485 SERVER_START_REQ( remove_hook )
489 ret = !wine_server_call_err( req );
490 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
493 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
498 /***********************************************************************
499 * CallNextHookEx (USER32.@)
501 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
503 struct user_thread_info *thread_info = get_user_thread_info();
504 HOOKPROC proc = NULL;
505 WCHAR module[MAX_PATH];
507 DWORD pid = 0, tid = 0;
509 BOOL prev_unicode = FALSE, next_unicode = FALSE;
512 SERVER_START_REQ( get_next_hook )
514 req->handle = thread_info->hook;
515 req->event = EVENT_MIN;
516 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
517 if (!wine_server_call_err( req ))
519 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
520 handle = reply->next;
525 prev_unicode = reply->prev_unicode;
526 next_unicode = reply->next_unicode;
533 TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
534 tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
539 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
540 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
543 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
544 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
547 ERR("Unknown hook id %d\n", id);
554 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
555 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
557 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
559 HHOOK prev = thread_info->hook;
560 thread_info->hook = handle;
561 ret = call_hook( proc, id, code, wparam, lparam, prev_unicode, next_unicode );
562 thread_info->hook = prev;
569 /***********************************************************************
570 * CallMsgFilterA (USER32.@)
572 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
574 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
575 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
579 /***********************************************************************
580 * CallMsgFilterW (USER32.@)
582 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
584 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
585 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
589 /***********************************************************************
590 * SetWinEventHook [USER32.@]
592 * Set up an event hook for a set of events.
595 * event_min [I] Lowest event handled by pfnProc
596 * event_max [I] Highest event handled by pfnProc
597 * inst [I] DLL containing pfnProc
598 * proc [I] Callback event hook function
599 * pid [I] Process to get events from, or 0 for all processes
600 * tid [I] Thread to get events from, or 0 for all threads
601 * flags [I] Flags indicating the status of pfnProc
604 * Success: A handle representing the hook.
605 * Failure: A NULL handle.
607 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
608 HMODULE inst, WINEVENTPROC proc,
609 DWORD pid, DWORD tid, DWORD flags)
611 HWINEVENTHOOK handle = 0;
612 WCHAR module[MAX_PATH];
615 TRACE("%ld,%ld,%p,%p,%08lx,%04lx,%08lx\n", event_min, event_max, inst,
616 proc, pid, tid, flags);
620 if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
624 if ((flags & WINEVENT_INCONTEXT) && !inst)
626 SetLastError(ERROR_HOOK_NEEDS_HMOD);
630 if (event_min > event_max)
632 SetLastError(ERROR_INVALID_HOOK_FILTER);
636 /* FIXME: what if the tid or pid belongs to another process? */
637 if (tid) /* thread-local hook */
640 SERVER_START_REQ( set_hook )
642 req->id = WH_WINEVENT;
645 req->event_min = event_min;
646 req->event_max = event_max;
649 if (inst) /* make proc relative to the module base */
651 req->proc = (void *)((char *)proc - (char *)inst);
652 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
654 else req->proc = proc;
656 if (!wine_server_call_err( req ))
658 handle = reply->handle;
659 get_user_thread_info()->active_hooks = reply->active_hooks;
664 TRACE("-> %p\n", handle);
669 /***********************************************************************
670 * UnhookWinEvent [USER32.@]
672 * Remove an event hook for a set of events.
675 * hEventHook [I] Event hook to remove
678 * Success: TRUE. The event hook has been removed.
679 * Failure: FALSE, if hEventHook is invalid.
681 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
685 TRACE( "%p\n", hEventHook );
687 SERVER_START_REQ( remove_hook )
689 req->handle = hEventHook;
690 req->id = WH_WINEVENT;
691 ret = !wine_server_call_err( req );
692 if (ret) get_user_thread_info()->active_hooks = reply->active_hooks;
698 inline static BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
699 LONG child_id, struct hook_info *info)
701 struct user_thread_info *thread_info = get_user_thread_info();
704 if (!HOOK_IsHooked( id ))
706 TRACE( "skipping hook %s mask %x\n", hook_names[id-WH_MINHOOK], thread_info->active_hooks );
710 SERVER_START_REQ( start_hook_chain )
715 req->object_id = object_id;
716 req->child_id = child_id;
717 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
718 ret = !wine_server_call( req );
721 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
722 info->handle = reply->handle;
723 info->proc = reply->proc;
724 info->tid = reply->tid;
725 thread_info->active_hooks = reply->active_hooks;
729 return ret && (info->tid || info->proc);
732 inline static BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
733 LONG child_id, struct hook_info *info)
737 SERVER_START_REQ( get_next_hook )
739 req->handle = info->handle;
742 req->object_id = object_id;
743 req->child_id = child_id;
744 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
745 ret = !wine_server_call( req );
748 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
749 info->handle = reply->next;
750 info->proc = reply->proc;
751 info->tid = reply->tid;
758 inline static void find_hook_close(DWORD id)
760 SERVER_START_REQ( finish_hook_chain )
763 wine_server_call( req );
768 /***********************************************************************
769 * NotifyWinEvent [USER32.@]
771 * Inform the OS that an event has occurred.
774 * event [I] Id of the event
775 * hwnd [I] Window holding the object that created the event
776 * object_id [I] Type of object that created the event
777 * child_id [I] Child object of nId, or CHILDID_SELF.
782 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
784 struct hook_info info;
786 TRACE("%04lx,%p,%ld,%ld\n", event, hwnd, object_id, child_id);
790 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
797 if (event & 0x80000000)
799 /* FIXME: on 64-bit platforms we need to invent some other way for
800 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
801 * struct call_hook *hook = (LRESULT *)hWnd;
802 * wparam = hook->wparam;
803 * lparam = hook->lparam;
805 LRESULT *ret = (LRESULT *)hwnd;
806 INT id, code, unicode;
808 id = (dwEvent & 0x7fff0000) >> 16;
809 code = event & 0x7fff;
810 unicode = event & 0x8000;
811 *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
816 if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
822 TRACE( "calling WH_WINEVENT hook %p event %lx hwnd %p %lx %lx module %s\n",
823 info.proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
825 if (!info.module[0] || (info.proc = get_hook_proc( info.proc, info.module )) != NULL)
828 DPRINTF( "%04lx:Call winevent hook proc %p (hhook=%p,event=%lx,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04lx,time=%lx)\n",
829 GetCurrentThreadId(), info.proc, info.handle, event, hwnd, object_id,
830 child_id, GetCurrentThreadId(), GetCurrentTime());
832 info.proc(info.handle, event, hwnd, object_id, child_id,
833 GetCurrentThreadId(), GetCurrentTime());
836 DPRINTF( "%04lx:Ret winevent hook proc %p (hhook=%p,event=%lx,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04lx,time=%lx)\n",
837 GetCurrentThreadId(), info.proc, info.handle, event, hwnd, object_id,
838 child_id, GetCurrentThreadId(), GetCurrentTime());
844 while (find_next_hook(event, hwnd, object_id, child_id, &info));
846 find_hook_close(WH_WINEVENT);
850 /***********************************************************************
851 * IsWinEventHookInstalled [USER32.@]
853 * Determine if an event hook is installed for an event.
856 * dwEvent [I] Id of the event
859 * TRUE, If there are any hooks installed for the event.
865 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
867 FIXME("(%ld)-stub!\n", dwEvent);