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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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);
91 WCHAR module[MAX_PATH];
94 #define WH_WINEVENT (WH_MAXHOOK+1)
96 static const char * const hook_names[WH_WINEVENT - WH_MINHOOK + 1] =
100 "WH_JOURNALPLAYBACK",
118 /***********************************************************************
119 * get_ll_hook_timeout
122 static UINT get_ll_hook_timeout(void)
124 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
129 /***********************************************************************
132 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
134 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
137 WCHAR module[MAX_PATH];
142 SetLastError( ERROR_INVALID_FILTER_PROC );
148 SetLastError( ERROR_INVALID_FILTER_PROC );
152 /* FIXME: what if the tid belongs to another process? */
153 if (tid) /* thread-local hook */
155 if (id == WH_JOURNALRECORD ||
156 id == WH_JOURNALPLAYBACK ||
157 id == WH_KEYBOARD_LL ||
159 id == WH_SYSMSGFILTER)
161 /* these can only be global */
162 SetLastError( ERROR_INVALID_PARAMETER );
167 else /* system-global hook */
169 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
170 else if (!inst || !(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH)
172 SetLastError( ERROR_HOOK_NEEDS_HMOD );
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 = (void *)((char *)proc - (char *)inst);
189 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
191 else req->proc = proc;
193 if (!wine_server_call_err( req )) handle = reply->handle;
197 TRACE( "%s %p %lx -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
202 /***********************************************************************
205 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
208 UNICODE_STRING usBuffer;
209 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
212 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
213 CBT_CREATEWNDW cbtcwW;
217 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
218 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
220 if (HIWORD(cbtcwA->lpcs->lpszName))
222 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
223 csW.lpszName = usBuffer.Buffer;
225 if (HIWORD(cbtcwA->lpcs->lpszClass))
227 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
228 csW.lpszClass = usBuffer.Buffer;
230 ret = proc( code, wparam, (LPARAM)&cbtcwW );
231 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
232 if (HIWORD(csW.lpszName)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszName );
233 if (HIWORD(csW.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszClass );
239 /***********************************************************************
242 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
246 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
249 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
250 CBT_CREATEWNDA cbtcwA;
255 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
256 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
258 if (HIWORD(cbtcwW->lpcs->lpszName)) {
259 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, NULL, 0, NULL, NULL );
260 csA.lpszName = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
261 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszName, -1, (LPSTR)csA.lpszName, len, NULL, NULL );
264 if (HIWORD(cbtcwW->lpcs->lpszClass)) {
265 len = WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, NULL, 0, NULL, NULL );
266 csA.lpszClass = HeapAlloc( GetProcessHeap(), 0, len*sizeof(CHAR) );
267 WideCharToMultiByte( CP_ACP, 0, cbtcwW->lpcs->lpszClass, -1, (LPSTR)csA.lpszClass, len, NULL, NULL );
270 ret = proc( code, wparam, (LPARAM)&cbtcwA );
271 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
272 if (HIWORD(csA.lpszName)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszName );
273 if (HIWORD(csA.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszClass );
279 /***********************************************************************
282 static LRESULT call_hook( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
283 BOOL prev_unicode, BOOL next_unicode )
288 DPRINTF( "%04lx:Call hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx)\n",
289 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
291 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
292 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
293 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
296 DPRINTF( "%04lx:Ret hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx) retval=%08lx\n",
297 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
303 /***********************************************************************
306 * Retrieve the hook procedure real value for a module-relative proc
308 static void *get_hook_proc( void *proc, const WCHAR *module )
312 if (!(mod = GetModuleHandleW(module)))
314 TRACE( "loading %s\n", debugstr_w(module) );
315 /* FIXME: the library will never be freed */
316 if (!(mod = LoadLibraryW(module))) return NULL;
318 return (char *)mod + (ULONG_PTR)proc;
322 /***********************************************************************
325 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
327 MESSAGEQUEUE *queue = QUEUE_Current();
328 HOOKPROC proc = NULL;
330 DWORD pid = 0, tid = 0;
331 WCHAR module[MAX_PATH];
332 BOOL unicode_hook = FALSE;
337 if (!queue) return 0;
338 SERVER_START_REQ( start_hook_chain )
341 req->event = EVENT_MIN;
342 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
343 if (!wine_server_call( req ))
345 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
346 handle = reply->handle;
350 unicode_hook = reply->unicode;
357 TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
358 tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
363 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
364 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
367 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
368 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
371 ERR("Unknown hook id %d\n", id);
378 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
379 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
381 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
383 HHOOK prev = queue->hook;
384 queue->hook = handle;
385 ret = call_hook( proc, id, code, wparam, lparam, unicode, unicode_hook );
392 SERVER_START_REQ( finish_hook_chain )
395 wine_server_call( req );
402 /***********************************************************************
405 BOOL HOOK_IsHooked( INT id )
407 return TRUE; /* FIXME */
411 /***********************************************************************
412 * SetWindowsHookA (USER32.@)
414 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
416 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
420 /***********************************************************************
421 * SetWindowsHookW (USER32.@)
423 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
425 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
429 /***********************************************************************
430 * SetWindowsHookExA (USER32.@)
432 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
434 return set_windows_hook( id, proc, inst, tid, FALSE );
437 /***********************************************************************
438 * SetWindowsHookExW (USER32.@)
440 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
442 return set_windows_hook( id, proc, inst, tid, TRUE );
446 /***********************************************************************
447 * UnhookWindowsHook (USER32.@)
449 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
453 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
455 SERVER_START_REQ( remove_hook )
460 ret = !wine_server_call_err( req );
463 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
469 /***********************************************************************
470 * UnhookWindowsHookEx (USER32.@)
472 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
476 TRACE( "%p\n", hhook );
478 SERVER_START_REQ( remove_hook )
482 ret = !wine_server_call_err( req );
485 if (!ret && GetLastError() == ERROR_INVALID_HANDLE) SetLastError( ERROR_INVALID_HOOK_HANDLE );
490 /***********************************************************************
491 * CallNextHookEx (USER32.@)
493 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
495 MESSAGEQUEUE *queue = QUEUE_Current();
496 HOOKPROC proc = NULL;
497 WCHAR module[MAX_PATH];
499 DWORD pid = 0, tid = 0;
501 BOOL prev_unicode = FALSE, next_unicode = FALSE;
504 if (!queue) return 0;
506 SERVER_START_REQ( get_next_hook )
508 req->handle = queue->hook;
509 req->event = EVENT_MIN;
510 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
511 if (!wine_server_call_err( req ))
513 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
514 handle = reply->next;
519 prev_unicode = reply->prev_unicode;
520 next_unicode = reply->next_unicode;
527 TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
528 tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
533 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
534 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
537 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
538 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
541 ERR("Unknown hook id %d\n", id);
548 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
549 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
551 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
553 HHOOK prev = queue->hook;
554 queue->hook = handle;
555 ret = call_hook( proc, id, code, wparam, lparam, prev_unicode, next_unicode );
563 /***********************************************************************
564 * CallMsgFilterA (USER32.@)
566 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
568 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
569 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
573 /***********************************************************************
574 * CallMsgFilterW (USER32.@)
576 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
578 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
579 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
583 /***********************************************************************
584 * SetWinEventHook [USER32.@]
586 * Set up an event hook for a set of events.
589 * event_min [I] Lowest event handled by pfnProc
590 * event_max [I] Highest event handled by pfnProc
591 * inst [I] DLL containing pfnProc
592 * proc [I] Callback event hook function
593 * pid [I] Process to get events from, or 0 for all processes
594 * tid [I] Thread to get events from, or 0 for all threads
595 * flags [I] Flags indicating the status of pfnProc
598 * Success: A handle representing the hook.
599 * Failure: A NULL handle.
601 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD event_min, DWORD event_max,
602 HMODULE inst, WINEVENTPROC proc,
603 DWORD pid, DWORD tid, DWORD flags)
605 HWINEVENTHOOK handle = 0;
606 WCHAR module[MAX_PATH];
609 TRACE("%ld,%ld,%p,%p,%08lx,%04lx,%08lx\n", event_min, event_max, inst,
610 proc, pid, tid, flags);
614 if (!(len = GetModuleFileNameW(inst, module, MAX_PATH)) || len >= MAX_PATH)
618 if ((flags & WINEVENT_INCONTEXT) && !inst)
620 SetLastError(ERROR_HOOK_NEEDS_HMOD);
624 if (event_min > event_max)
626 SetLastError(ERROR_INVALID_HOOK_FILTER);
630 /* FIXME: what if the tid or pid belongs to another process? */
631 if (tid) /* thread-local hook */
634 SERVER_START_REQ( set_hook )
636 req->id = WH_WINEVENT;
639 req->event_min = event_min;
640 req->event_max = event_max;
643 if (inst) /* make proc relative to the module base */
645 req->proc = (void *)((char *)proc - (char *)inst);
646 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
648 else req->proc = proc;
650 if (!wine_server_call_err( req )) handle = reply->handle;
654 TRACE("-> %p\n", handle);
659 /***********************************************************************
660 * UnhookWinEvent [USER32.@]
662 * Remove an event hook for a set of events.
665 * hEventHook [I] Event hook to remove
668 * Success: TRUE. The event hook has been removed.
669 * Failure: FALSE, if hEventHook is invalid.
671 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
675 TRACE( "%p\n", hEventHook );
677 SERVER_START_REQ( remove_hook )
679 req->handle = hEventHook;
680 req->id = WH_WINEVENT;
681 ret = !wine_server_call_err( req );
687 inline static BOOL find_first_hook(DWORD id, DWORD event, HWND hwnd, LONG object_id,
688 LONG child_id, struct hook_info *info)
692 SERVER_START_REQ( start_hook_chain )
697 req->object_id = object_id;
698 req->child_id = child_id;
699 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
700 ret = !wine_server_call( req );
703 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
704 info->handle = reply->handle;
705 info->proc = reply->proc;
706 info->tid = reply->tid;
710 return ret && (info->tid || info->proc);
713 inline static BOOL find_next_hook(DWORD event, HWND hwnd, LONG object_id,
714 LONG child_id, struct hook_info *info)
718 SERVER_START_REQ( get_next_hook )
720 req->handle = info->handle;
723 req->object_id = object_id;
724 req->child_id = child_id;
725 wine_server_set_reply( req, info->module, sizeof(info->module)-sizeof(WCHAR) );
726 ret = !wine_server_call( req );
729 info->module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
730 info->handle = reply->next;
731 info->proc = reply->proc;
732 info->tid = reply->tid;
739 inline static void find_hook_close(DWORD id)
741 SERVER_START_REQ( finish_hook_chain )
744 wine_server_call( req );
749 /***********************************************************************
750 * NotifyWinEvent [USER32.@]
752 * Inform the OS that an event has occurred.
755 * event [I] Id of the event
756 * hwnd [I] Window holding the object that created the event
757 * object_id [I] Type of object that created the event
758 * child_id [I] Child object of nId, or CHILDID_SELF.
763 void WINAPI NotifyWinEvent(DWORD event, HWND hwnd, LONG object_id, LONG child_id)
765 struct hook_info info;
767 TRACE("%04lx,%p,%ld,%ld\n", event, hwnd, object_id, child_id);
771 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
778 if (event & 0x80000000)
780 /* FIXME: on 64-bit platforms we need to invent some other way for
781 * passing parameters, nId and nChildId can't hold full [W|L]PARAM.
782 * struct call_hook *hook = (LRESULT *)hWnd;
783 * wparam = hook->wparam;
784 * lparam = hook->lparam;
786 LRESULT *ret = (LRESULT *)hwnd;
787 INT id, code, unicode;
789 id = (dwEvent & 0x7fff0000) >> 16;
790 code = event & 0x7fff;
791 unicode = event & 0x8000;
792 *ret = HOOK_CallHooks(id, code, object_id, child_id, unicode);
797 if (!find_first_hook(WH_WINEVENT, event, hwnd, object_id, child_id, &info)) return;
803 TRACE( "calling WH_WINEVENT hook %p event %lx hwnd %p %lx %lx module %s\n",
804 info.proc, event, hwnd, object_id, child_id, debugstr_w(info.module) );
806 if (!info.module[0] || (info.proc = get_hook_proc( info.proc, info.module )) != NULL)
809 DPRINTF( "%04lx:Call winevent hook proc %p (hhook=%p,event=%lx,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04lx,time=%lx)\n",
810 GetCurrentThreadId(), info.proc, info.handle, event, hwnd, object_id,
811 child_id, GetCurrentThreadId(), GetCurrentTime());
813 info.proc(info.handle, event, hwnd, object_id, child_id,
814 GetCurrentThreadId(), GetCurrentTime());
817 DPRINTF( "%04lx:Ret winevent hook proc %p (hhook=%p,event=%lx,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04lx,time=%lx)\n",
818 GetCurrentThreadId(), info.proc, info.handle, event, hwnd, object_id,
819 child_id, GetCurrentThreadId(), GetCurrentTime());
825 while (find_next_hook(event, hwnd, object_id, child_id, &info));
827 find_hook_close(WH_WINEVENT);
831 /***********************************************************************
832 * IsWinEventHookInstalled [USER32.@]
834 * Determine if an event hook is installed for an event.
837 * dwEvent [I] Id of the event
840 * TRUE, If there are any hooks installed for the event.
846 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
848 FIXME("(%ld)-stub!\n", dwEvent);