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
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 "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);
85 static const char * const hook_names[WH_MAXHOOK - WH_MINHOOK + 1] =
106 /***********************************************************************
107 * get_ll_hook_timeout
110 static UINT get_ll_hook_timeout(void)
112 /* FIXME: should retrieve LowLevelHooksTimeout in HKEY_CURRENT_USER\Control Panel\Desktop */
117 /***********************************************************************
120 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
122 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
125 WCHAR module[MAX_PATH];
128 if (tid) /* thread-local hook */
130 if (id == WH_JOURNALRECORD ||
131 id == WH_JOURNALPLAYBACK ||
132 id == WH_KEYBOARD_LL ||
134 id == WH_SYSMSGFILTER)
136 /* these can only be global */
137 SetLastError( ERROR_INVALID_PARAMETER );
142 else /* system-global hook */
144 if (id == WH_KEYBOARD_LL || id == WH_MOUSE_LL) inst = 0;
145 else if (!inst || !(len = GetModuleFileNameW( inst, module, MAX_PATH )) || len >= MAX_PATH)
147 SetLastError( ERROR_INVALID_PARAMETER );
152 SERVER_START_REQ( set_hook )
156 req->unicode = unicode;
157 if (inst) /* make proc relative to the module base */
159 req->proc = (void *)((char *)proc - (char *)inst);
160 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
162 else req->proc = proc;
164 if (!wine_server_call_err( req )) handle = reply->handle;
168 TRACE( "%s %p %lx -> %p\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
173 /***********************************************************************
176 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
179 UNICODE_STRING usBuffer;
180 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
183 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
184 CBT_CREATEWNDW cbtcwW;
188 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
189 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
191 if (HIWORD(cbtcwA->lpcs->lpszName))
193 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszName);
194 csW.lpszName = usBuffer.Buffer;
196 if (HIWORD(cbtcwA->lpcs->lpszClass))
198 RtlCreateUnicodeStringFromAsciiz(&usBuffer,cbtcwA->lpcs->lpszClass);
199 csW.lpszClass = usBuffer.Buffer;
201 ret = proc( code, wparam, (LPARAM)&cbtcwW );
202 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
203 if (HIWORD(csW.lpszName)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszName );
204 if (HIWORD(csW.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszClass );
210 /***********************************************************************
213 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
217 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
220 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
221 CBT_CREATEWNDA cbtcwA;
225 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
226 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
228 if (HIWORD(cbtcwW->lpcs->lpszName))
229 csA.lpszName = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszName );
230 if (HIWORD(cbtcwW->lpcs->lpszClass))
231 csA.lpszClass = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszClass );
232 ret = proc( code, wparam, (LPARAM)&cbtcwA );
233 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
234 if (HIWORD(csA.lpszName)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszName );
235 if (HIWORD(csA.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszClass );
241 /***********************************************************************
244 static LRESULT call_hook( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
245 BOOL prev_unicode, BOOL next_unicode )
250 DPRINTF( "%04lx:Call hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx)\n",
251 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
253 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
254 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
255 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
258 DPRINTF( "%04lx:Ret hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx) retval=%08lx\n",
259 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
265 /***********************************************************************
268 * Retrieve the hook procedure real value for a module-relative proc
270 static HOOKPROC get_hook_proc( HOOKPROC proc, const WCHAR *module )
274 if (!(mod = GetModuleHandleW(module)))
276 TRACE( "loading %s\n", debugstr_w(module) );
277 /* FIXME: the library will never be freed */
278 if (!(mod = LoadLibraryW(module))) return NULL;
280 return (HOOKPROC)((char *)mod + (ULONG_PTR)proc);
284 /***********************************************************************
287 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
289 MESSAGEQUEUE *queue = QUEUE_Current();
290 HOOKPROC proc = NULL;
292 DWORD pid = 0, tid = 0;
293 WCHAR module[MAX_PATH];
294 BOOL unicode_hook = FALSE;
297 if (!queue) return 0;
298 SERVER_START_REQ( start_hook_chain )
301 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
302 if (!wine_server_call( req ))
304 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
305 handle = reply->handle;
309 unicode_hook = reply->unicode;
316 TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
317 tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
322 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
323 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
326 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
327 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
333 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
334 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
336 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
338 int locks = WIN_SuspendWndsLock();
339 HHOOK prev = queue->hook;
340 queue->hook = handle;
341 ret = call_hook( proc, id, code, wparam, lparam, unicode, unicode_hook );
343 WIN_RestoreWndsLock( locks );
349 SERVER_START_REQ( finish_hook_chain )
352 wine_server_call( req );
359 /***********************************************************************
362 BOOL HOOK_IsHooked( INT id )
364 return TRUE; /* FIXME */
368 /***********************************************************************
369 * SetWindowsHookA (USER32.@)
371 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
373 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
377 /***********************************************************************
378 * SetWindowsHookW (USER32.@)
380 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
382 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
386 /***********************************************************************
387 * SetWindowsHookExA (USER32.@)
389 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
391 return set_windows_hook( id, proc, inst, tid, FALSE );
394 /***********************************************************************
395 * SetWindowsHookExW (USER32.@)
397 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
399 return set_windows_hook( id, proc, inst, tid, TRUE );
403 /***********************************************************************
404 * UnhookWindowsHook (USER32.@)
406 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
410 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
412 SERVER_START_REQ( remove_hook )
417 ret = !wine_server_call_err( req );
425 /***********************************************************************
426 * UnhookWindowsHookEx (USER32.@)
428 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
432 TRACE( "%p\n", hhook );
434 SERVER_START_REQ( remove_hook )
437 ret = !wine_server_call_err( req );
444 /***********************************************************************
445 * CallNextHookEx (USER32.@)
447 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
449 MESSAGEQUEUE *queue = QUEUE_Current();
450 HOOKPROC proc = NULL;
451 WCHAR module[MAX_PATH];
453 DWORD pid = 0, tid = 0;
455 BOOL prev_unicode = FALSE, next_unicode = FALSE;
458 if (!queue) return 0;
460 SERVER_START_REQ( get_next_hook )
462 req->handle = queue->hook;
463 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
464 if (!wine_server_call_err( req ))
466 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
467 handle = reply->next;
472 prev_unicode = reply->prev_unicode;
473 next_unicode = reply->next_unicode;
480 TRACE( "calling hook in thread %04lx %s code %x wp %x lp %lx\n",
481 tid, hook_names[id-WH_MINHOOK], code, wparam, lparam );
486 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_KEYBOARD_LL_HOOK, wparam, lparam,
487 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
490 MSG_SendInternalMessageTimeout( pid, tid, WM_WINE_MOUSE_LL_HOOK, wparam, lparam,
491 SMTO_ABORTIFHUNG, get_ll_hook_timeout(), &ret );
497 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
498 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
500 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
502 HHOOK prev = queue->hook;
503 queue->hook = handle;
504 ret = call_hook( proc, id, code, wparam, lparam, prev_unicode, next_unicode );
512 /***********************************************************************
513 * CallMsgFilterA (USER32.@)
515 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
517 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
518 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
522 /***********************************************************************
523 * CallMsgFilterW (USER32.@)
525 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
527 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
528 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
532 /***********************************************************************
533 * SetWinEventHook [USER32.@]
535 * Set up an event hook for a set of events.
538 * dwMin [I] Lowest event handled by pfnProc
539 * dwMax [I] Highest event handled by pfnProc
540 * hModule [I] DLL containing pfnProc
541 * pfnProc [I] Callback event hook function
542 * dwProcess [I] Process to get events from, or 0 for all processes
543 * dwThread [I] Thread to get events from, or 0 for all threads
544 * dwFlags [I] Flags indicating the status of pfnProc
547 * Success: A handle representing the hook.
548 * Failure: A NULL handle.
553 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD dwMin, DWORD dwMax, HMODULE hModule,
554 WINEVENTPROC pfnProc, DWORD dwProcess,
555 DWORD dwThread, DWORD dwFlags)
557 FIXME("(%ld,%ld,%p,%p,%ld,%ld,0x%08lx)-stub!\n", dwMin, dwMax, hModule,
558 pfnProc, dwProcess, dwThread, dwFlags);
563 /***********************************************************************
564 * UnhookWinEvent [USER32.@]
566 * Remove an event hook for a set of events.
569 * hEventHook [I] Event hook to remove
572 * Success: TRUE. The event hook has been removed.
573 * Failure: FALSE, if hEventHook is invalid.
578 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
580 FIXME("(%p)-stub!\n", hEventHook);
582 return (hEventHook != 0);
586 /***********************************************************************
587 * NotifyWinEvent [USER32.@]
589 * Inform the OS that an event has occurred.
592 * dwEvent [I] Id of the event
593 * hWnd [I] Window holding the object that created the event
594 * nId [I] Type of object that created the event
595 * nChildId [I] Child object of nId, or CHILDID_SELF.
603 void WINAPI NotifyWinEvent(DWORD dwEvent, HWND hWnd, LONG nId, LONG nChildId)
605 FIXME("(%ld,%p,%ld,%ld)-stub!\n", dwEvent, hWnd, nId, nChildId);
609 /***********************************************************************
610 * IsWinEventHookInstalled [USER32.@]
612 * Determine if an event hook is installed for an event.
615 * dwEvent [I] Id of the event
618 * TRUE, If there are any hooks installed for the event.
624 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
626 FIXME("(%ld)-stub!\n", dwEvent);