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
71 #include "wine/server.h"
72 #include "wine/unicode.h"
73 #include "wine/debug.h"
75 WINE_DEFAULT_DEBUG_CHANNEL(hook);
76 WINE_DECLARE_DEBUG_CHANNEL(relay);
78 static const char * const hook_names[WH_MAXHOOK - WH_MINHOOK + 1] =
99 /***********************************************************************
102 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
104 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
107 WCHAR module[MAX_PATH];
109 if (tid) /* thread-local hook */
111 if (id == WH_JOURNALRECORD ||
112 id == WH_JOURNALPLAYBACK ||
113 id == WH_KEYBOARD_LL ||
115 id == WH_SYSMSGFILTER)
117 /* these can only be global */
118 SetLastError( ERROR_INVALID_PARAMETER );
123 else /* system-global hook */
125 if (!inst || !GetModuleFileNameW( inst, module, MAX_PATH ))
127 SetLastError( ERROR_INVALID_PARAMETER );
132 SERVER_START_REQ( set_hook )
136 req->unicode = unicode;
137 if (inst) /* make proc relative to the module base */
139 req->proc = (void *)((char *)proc - (char *)inst);
140 wine_server_add_data( req, module, strlenW(module) * sizeof(WCHAR) );
142 else req->proc = proc;
144 if (!wine_server_call_err( req )) handle = reply->handle;
148 TRACE( "%s %p %lx -> %x\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
153 /***********************************************************************
156 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
160 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
163 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
164 CBT_CREATEWNDW cbtcwW;
168 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
169 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
171 if (HIWORD(cbtcwA->lpcs->lpszName))
172 csW.lpszName = HEAP_strdupAtoW( GetProcessHeap(), 0, cbtcwA->lpcs->lpszName );
173 if (HIWORD(cbtcwA->lpcs->lpszClass))
174 csW.lpszClass = HEAP_strdupAtoW( GetProcessHeap(), 0, cbtcwA->lpcs->lpszClass );
175 ret = proc( code, wparam, (LPARAM)&cbtcwW );
176 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
177 if (HIWORD(csW.lpszName)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszName );
178 if (HIWORD(csW.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszClass );
184 /***********************************************************************
187 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
191 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
194 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
195 CBT_CREATEWNDA cbtcwA;
199 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
200 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
202 if (HIWORD(cbtcwW->lpcs->lpszName))
203 csA.lpszName = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszName );
204 if (HIWORD(cbtcwW->lpcs->lpszClass))
205 csA.lpszClass = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszClass );
206 ret = proc( code, wparam, (LPARAM)&cbtcwA );
207 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
208 if (HIWORD(csA.lpszName)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszName );
209 if (HIWORD(csA.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszClass );
215 /***********************************************************************
218 static LRESULT call_hook( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
219 BOOL prev_unicode, BOOL next_unicode )
224 DPRINTF( "%08lx:Call hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx)\n",
225 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
227 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
228 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
229 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
232 DPRINTF( "%08lx:Ret hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx) retval=%08lx\n",
233 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
239 /***********************************************************************
242 * Retrieve the hook procedure real value for a module-relative proc
244 static HOOKPROC get_hook_proc( HOOKPROC proc, const WCHAR *module )
248 if (!(mod = GetModuleHandleW(module)))
250 TRACE( "loading %s\n", debugstr_w(module) );
251 /* FIXME: the library will never be freed */
252 if (!(mod = LoadLibraryW(module))) return NULL;
254 return (HOOKPROC)((char *)mod + (ULONG_PTR)proc);
258 /***********************************************************************
261 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
263 MESSAGEQUEUE *queue = QUEUE_Current();
264 HOOKPROC proc = NULL;
266 WCHAR module[MAX_PATH];
267 BOOL unicode_hook = FALSE;
270 if (!queue) return 0;
272 SERVER_START_REQ( start_hook_chain )
275 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
276 if (!wine_server_call_err( req ))
278 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
279 queue->hook = reply->handle;
281 unicode_hook = reply->unicode;
288 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
289 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
291 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
293 int locks = WIN_SuspendWndsLock();
294 ret = call_hook( proc, id, code, wparam, lparam, unicode, unicode_hook );
295 WIN_RestoreWndsLock( locks );
298 SERVER_START_REQ( finish_hook_chain )
301 wine_server_call( req );
311 /***********************************************************************
314 BOOL HOOK_IsHooked( INT id )
316 return TRUE; /* FIXME */
320 /***********************************************************************
321 * SetWindowsHookA (USER32.@)
323 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
325 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
329 /***********************************************************************
330 * SetWindowsHookW (USER32.@)
332 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
334 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
338 /***********************************************************************
339 * SetWindowsHookExA (USER32.@)
341 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
343 return set_windows_hook( id, proc, inst, tid, FALSE );
346 /***********************************************************************
347 * SetWindowsHookExW (USER32.@)
349 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
351 return set_windows_hook( id, proc, inst, tid, TRUE );
355 /***********************************************************************
356 * UnhookWindowsHook (USER32.@)
358 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
362 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
364 SERVER_START_REQ( remove_hook )
368 ret = !wine_server_call_err( req );
376 /***********************************************************************
377 * UnhookWindowsHookEx (USER32.@)
379 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
383 TRACE( "%x\n", hhook );
385 SERVER_START_REQ( remove_hook )
388 ret = !wine_server_call_err( req );
395 /***********************************************************************
396 * CallNextHookEx (USER32.@)
398 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
400 MESSAGEQUEUE *queue = QUEUE_Current();
402 WCHAR module[MAX_PATH];
403 HOOKPROC proc = NULL;
405 BOOL prev_unicode = FALSE, next_unicode = FALSE;
408 if (!queue) return 0;
410 SERVER_START_REQ( get_next_hook )
413 wine_server_set_reply( req, module, sizeof(module)-sizeof(WCHAR) );
414 if (!wine_server_call_err( req ))
416 module[wine_server_reply_size(req) / sizeof(WCHAR)] = 0;
417 queue->hook = reply->next;
420 prev_unicode = reply->prev_unicode;
421 next_unicode = reply->next_unicode;
428 TRACE( "calling hook %p %s code %x wp %x lp %lx module %s\n",
429 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, debugstr_w(module) );
431 if (!module[0] || (proc = get_hook_proc( proc, module )) != NULL)
432 return call_hook( proc, id, code, wparam, lparam, prev_unicode, next_unicode );
439 /***********************************************************************
440 * CallMsgFilterA (USER32.@)
442 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
444 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
445 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
449 /***********************************************************************
450 * CallMsgFilterW (USER32.@)
452 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
454 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
455 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
459 /***********************************************************************
460 * SetWinEventHook [USER32.@]
462 * Set up an event hook for a set of events.
465 * dwMin [I] Lowest event handled by pfnProc
466 * dwMax [I] Highest event handled by pfnProc
467 * hModule [I] DLL containing pfnProc
468 * pfnProc [I] Callback event hook function
469 * dwProcess [I] Process to get events from, or 0 for all processes
470 * dwThread [I] Thread to get events from, or 0 for all threads
471 * dwFlags [I] Flags indicating the status of pfnProc
474 * Success: A handle representing the hook.
475 * Failure: A NULL handle.
480 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD dwMin, DWORD dwMax, HMODULE hModule,
481 WINEVENTPROC pfnProc, DWORD dwProcess,
482 DWORD dwThread, DWORD dwFlags)
484 FIXME("(%ld,%ld,0x%08x,%p,%ld,%ld,0x%08lx)-stub!\n", dwMin, dwMax, hModule,
485 pfnProc, dwProcess, dwThread, dwFlags);
490 /***********************************************************************
491 * UnhookWinEvent [USER32.@]
493 * Remove an event hook for a set of events.
496 * hEventHook [I] Event hook to remove
499 * Success: TRUE. The event hook has been removed.
500 * Failure: FALSE, if hEventHook is invalid.
505 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
507 FIXME("(%x)-stub!\n", hEventHook);
509 return (hEventHook != 0);
513 /***********************************************************************
514 * NotifyWinEvent [USER32.@]
516 * Inform the OS that an event has occurred.
519 * dwEvent [I] Id of the event
520 * hWnd [I] Window holding the object that created the event
521 * nId [I] Type of object that created the event
522 * nChildId [I] Child object of nId, or CHILDID_SELF.
530 void WINAPI NotifyWinEvent(DWORD dwEvent, HWND hWnd, LONG nId, LONG nChildId)
532 FIXME("(%ld,0x%08x,%ld,%ld)-stub!\n", dwEvent, hWnd, nId, nChildId);
536 /***********************************************************************
537 * IsWinEventHookInstalled [USER32.@]
539 * Determine if an event hook is installed for an event.
542 * dwEvent [I] Id of the event
545 * TRUE, If there are any hooks installed for the event.
551 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
553 FIXME("(%ld)-stub!\n", dwEvent);