2 * Windows hook functions
4 * Copyright 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Status of the various hooks:
23 * WH_JOURNALRECORD Partially implemented
24 * WH_JOURNALPLAYBACK Partially implemented
26 * WH_GETMESSAGE OK (FIXME: A/W mapping?)
27 * WH_CALLWNDPROC OK (FIXME: A/W mapping?)
35 * HCBT_CLICKSKIPPED OK
37 * HCBT_SYSCOMMAND Not implemented
41 * WH_HARDWARE Not supported in Win32
42 * WH_DEBUG Not implemented
44 * HSHELL_WINDOWCREATED OK
45 * HSHELL_WINDOWDESTROYED OK
46 * HSHELL_ACTIVATESHELLWINDOW Not implemented
47 * HSHELL_WINDOWACTIVATED Not implemented
48 * HSHELL_GETMINRECT Not implemented
49 * HSHELL_REDRAW Not implemented
50 * HSHELL_TASKMAN Not implemented
51 * HSHELL_LANGUAGE Not implemented
52 * HSHELL_SYSMENU Not implemented
53 * HSHELL_ENDTASK Not implemented
54 * HSHELL_ACCESSIBILITYSTATE Not implemented
55 * HSHELL_APPCOMMAND Not implemented
56 * HSHELL_WINDOWREPLACED Not implemented
57 * HSHELL_WINDOWREPLACING Not implemented
58 * WH_FOREGROUNDIDLE Not implemented
59 * WH_CALLWNDPROCRET OK (FIXME: A/W mapping?)
60 * WH_KEYBOARD_LL Implemented but should use SendMessage instead
61 * WH_MOUSE_LL Implemented but should use SendMessage instead
65 #include "wine/port.h"
76 #include "wine/server.h"
77 #include "wine/unicode.h"
78 #include "wine/debug.h"
81 WINE_DEFAULT_DEBUG_CHANNEL(hook);
82 WINE_DECLARE_DEBUG_CHANNEL(relay);
84 static const char * const hook_names[WH_MAXHOOK - WH_MINHOOK + 1] =
105 /***********************************************************************
106 * get_ll_hook_timeout
109 static UINT get_ll_hook_timeout(void)
111 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
116 /***********************************************************************
119 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
121 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
124 WCHAR module[MAX_PATH];
127 if (tid) /* thread-local hook */
129 if (id == WH_JOURNALRECORD ||
130 id == WH_JOURNALPLAYBACK ||
131 id == WH_KEYBOARD_LL ||
133 id == WH_SYSMSGFILTER)
135 /* these can only be global */
136 SetLastError( ERROR_INVALID_PARAMETER );
141 else /* system-global hook */
143 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
144 else if (!inst || !(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH)
146 SetLastError( ERROR_INVALID_PARAMETER );
151 SERVER_START_REQ( set_hook )
155 req->unicode = unicode;
156 if (inst) /* make proc relative to the module base */
158 req->proc = (void *)((char *)proc - (char *)inst);
159 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
161 else req->proc = proc;
163 if (!wine_server_call_err( req )) handle = reply->handle;
167 TRACE( "%s %p %lx -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
172 /***********************************************************************
175 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
178 UNICODE_STRING usBuffer;
179 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
182 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
183 CBT_CREATEWNDW cbtcwW;
187 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
188 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
190 if (HIWORD(cbtcwA->lpcs->lpszName))
192 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
193 csW.lpszName = usBuffer.Buffer;
195 if (HIWORD(cbtcwA->lpcs->lpszClass))
197 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
198 csW.lpszClass = usBuffer.Buffer;
200 ret = proc( code, wparam, (LPARAM)&cbtcwW );
201 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
202 if (HIWORD(csW.lpszName)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszName );
203 if (HIWORD(csW.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszClass );
209 /***********************************************************************
212 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
216 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
219 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
220 CBT_CREATEWNDA cbtcwA;
224 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
225 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
227 if (HIWORD(cbtcwW->lpcs->lpszName))
228 csA.lpszName = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszName );
229 if (HIWORD(cbtcwW->lpcs->lpszClass))
230 csA.lpszClass = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszClass );
231 ret = proc( code, wparam, (LPARAM)&cbtcwA );
232 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
233 if (HIWORD(csA.lpszName)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszName );
234 if (HIWORD(csA.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszClass );
240 /***********************************************************************
243 static LRESULT call_hook( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
244 BOOL prev_unicode, BOOL next_unicode )
249 DPRINTF( "%04lx:Call hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx)\n",
250 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
252 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
253 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
254 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
257 DPRINTF( "%04lx:Ret hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx) retval=%08lx\n",
258 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
264 /***********************************************************************
267 * Retrieve the hook procedure real value for a module-relative proc
269 static HOOKPROC get_hook_proc( HOOKPROC proc, const WCHAR *module )
273 if (!(mod = GetModuleHandleW(module)))
275 TRACE( "loading %s\n", debugstr_w(module) );
276 /* FIXME: the library will never be freed */
277 if (!(mod = LoadLibraryW(module))) return NULL;
279 return (HOOKPROC)((char *)mod + (ULONG_PTR)proc);
283 /***********************************************************************
286 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
288 MESSAGEQUEUE *queue = QUEUE_Current();
289 HOOKPROC proc = NULL;
291 DWORD pid = 0, tid = 0;
292 WCHAR module[MAX_PATH];
293 BOOL unicode_hook = FALSE;
296 if (!queue) return 0;
297 SERVER_START_REQ( start_hook_chain )
300 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
301 if (!wine_server_call( req ))
303 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
304 handle = reply->handle;
308 unicode_hook = reply->unicode;
315 TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
316 tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
321 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
322 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
325 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
326 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
332 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
333 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
335 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
337 int locks = WIN_SuspendWndsLock();
338 HHOOK prev = queue->hook;
339 queue->hook = handle;
340 ret = call_hook( proc, id, code, wparam, lparam, unicode, unicode_hook );
342 WIN_RestoreWndsLock( locks );
348 SERVER_START_REQ( finish_hook_chain )
351 wine_server_call( req );
358 /***********************************************************************
361 BOOL HOOK_IsHooked( INT id )
363 return TRUE; /* FIXME */
367 /***********************************************************************
368 * SetWindowsHookA (USER32.@)
370 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
372 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
376 /***********************************************************************
377 * SetWindowsHookW (USER32.@)
379 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
381 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
385 /***********************************************************************
386 * SetWindowsHookExA (USER32.@)
388 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
390 return set_windows_hook( id, proc, inst, tid, FALSE );
393 /***********************************************************************
394 * SetWindowsHookExW (USER32.@)
396 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
398 return set_windows_hook( id, proc, inst, tid, TRUE );
402 /***********************************************************************
403 * UnhookWindowsHook (USER32.@)
405 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
409 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
411 SERVER_START_REQ( remove_hook )
416 ret = !wine_server_call_err( req );
424 /***********************************************************************
425 * UnhookWindowsHookEx (USER32.@)
427 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
431 TRACE( "%p\n", hhook );
433 SERVER_START_REQ( remove_hook )
436 ret = !wine_server_call_err( req );
443 /***********************************************************************
444 * CallNextHookEx (USER32.@)
446 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
448 MESSAGEQUEUE *queue = QUEUE_Current();
449 HOOKPROC proc = NULL;
450 WCHAR module[MAX_PATH];
452 DWORD pid = 0, tid = 0;
454 BOOL prev_unicode = FALSE, next_unicode = FALSE;
457 if (!queue) return 0;
459 SERVER_START_REQ( get_next_hook )
461 req->handle = queue->hook;
462 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
463 if (!wine_server_call_err( req ))
465 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
466 handle = reply->next;
471 prev_unicode = reply->prev_unicode;
472 next_unicode = reply->next_unicode;
479 TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
480 tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
485 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
486 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
489 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
490 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
496 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
497 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
499 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
501 HHOOK prev = queue->hook;
502 queue->hook = handle;
503 ret = call_hook( proc, id, code, wparam, lparam, prev_unicode, next_unicode );
511 /***********************************************************************
512 * CallMsgFilterA (USER32.@)
514 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
516 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
517 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
521 /***********************************************************************
522 * CallMsgFilterW (USER32.@)
524 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
526 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
527 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
531 /***********************************************************************
532 * SetWinEventHook [USER32.@]
534 * Set up an event hook for a set of events.
537 * dwMin [I] Lowest event handled by pfnProc
538 * dwMax [I] Highest event handled by pfnProc
539 * hModule [I] DLL containing pfnProc
540 * pfnProc [I] Callback event hook function
541 * dwProcess [I] Process to get events from, or 0 for all processes
542 * dwThread [I] Thread to get events from, or 0 for all threads
543 * dwFlags [I] Flags indicating the status of pfnProc
546 * Success: A handle representing the hook.
547 * Failure: A NULL handle.
552 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD dwMin, DWORD dwMax, HMODULE hModule,
553 WINEVENTPROC pfnProc, DWORD dwProcess,
554 DWORD dwThread, DWORD dwFlags)
556 FIXME("(%ld,%ld,%p,%p,%ld,%ld,0x%08lx)-stub!\n", dwMin, dwMax, hModule,
557 pfnProc, dwProcess, dwThread, dwFlags);
562 /***********************************************************************
563 * UnhookWinEvent [USER32.@]
565 * Remove an event hook for a set of events.
568 * hEventHook [I] Event hook to remove
571 * Success: TRUE. The event hook has been removed.
572 * Failure: FALSE, if hEventHook is invalid.
577 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
579 FIXME("(%p)-stub!\n", hEventHook);
581 return (hEventHook != 0);
585 /***********************************************************************
586 * NotifyWinEvent [USER32.@]
588 * Inform the OS that an event has occurred.
591 * dwEvent [I] Id of the event
592 * hWnd [I] Window holding the object that created the event
593 * nId [I] Type of object that created the event
594 * nChildId [I] Child object of nId, or CHILDID_SELF.
602 void WINAPI NotifyWinEvent(DWORD dwEvent, HWND hWnd, LONG nId, LONG nChildId)
604 FIXME("(%ld,%p,%ld,%ld)-stub!\n", dwEvent, hWnd, nId, nChildId);
608 /***********************************************************************
609 * IsWinEventHookInstalled [USER32.@]
611 * Determine if an event hook is installed for an event.
614 * dwEvent [I] Id of the event
617 * TRUE, If there are any hooks installed for the event.
623 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
625 FIXME("(%ld)-stub!\n", dwEvent);