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/debug.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(hook);
75 WINE_DECLARE_DEBUG_CHANNEL(relay);
77 static const char * const hook_names[WH_MAXHOOK - WH_MINHOOK + 1] =
98 /***********************************************************************
101 * Implementation of SetWindowsHookExA and SetWindowsHookExW.
103 static HHOOK set_windows_hook( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid, BOOL unicode )
107 if (tid) /* thread-local hook */
109 if (id == WH_JOURNALRECORD ||
110 id == WH_JOURNALPLAYBACK ||
111 id == WH_KEYBOARD_LL ||
113 id == WH_SYSMSGFILTER)
115 /* these can only be global */
116 SetLastError( ERROR_INVALID_PARAMETER );
120 else /* system-global hook */
124 SetLastError( ERROR_INVALID_PARAMETER );
127 FIXME( "system hook %s won't work right\n", hook_names[id-WH_MINHOOK] );
130 SERVER_START_REQ( set_hook )
135 req->unicode = unicode;
136 if (!wine_server_call_err( req )) handle = reply->handle;
140 TRACE( "%s %p %lx -> %x\n", hook_names[id-WH_MINHOOK], proc, tid, handle );
145 /***********************************************************************
148 static LRESULT call_hook_AtoW( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
152 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
155 CBT_CREATEWNDA *cbtcwA = (CBT_CREATEWNDA *)lparam;
156 CBT_CREATEWNDW cbtcwW;
160 cbtcwW.hwndInsertAfter = cbtcwA->hwndInsertAfter;
161 csW = *(CREATESTRUCTW *)cbtcwA->lpcs;
163 if (HIWORD(cbtcwA->lpcs->lpszName))
164 csW.lpszName = HEAP_strdupAtoW( GetProcessHeap(), 0, cbtcwA->lpcs->lpszName );
165 if (HIWORD(cbtcwA->lpcs->lpszClass))
166 csW.lpszClass = HEAP_strdupAtoW( GetProcessHeap(), 0, cbtcwA->lpcs->lpszClass );
167 ret = proc( code, wparam, (LPARAM)&cbtcwW );
168 cbtcwA->hwndInsertAfter = cbtcwW.hwndInsertAfter;
169 if (HIWORD(csW.lpszName)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszName );
170 if (HIWORD(csW.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPWSTR)csW.lpszClass );
176 /***********************************************************************
179 static LRESULT call_hook_WtoA( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam )
183 if (id != WH_CBT || code != HCBT_CREATEWND) ret = proc( code, wparam, lparam );
186 CBT_CREATEWNDW *cbtcwW = (CBT_CREATEWNDW *)lparam;
187 CBT_CREATEWNDA cbtcwA;
191 cbtcwA.hwndInsertAfter = cbtcwW->hwndInsertAfter;
192 csA = *(CREATESTRUCTA *)cbtcwW->lpcs;
194 if (HIWORD(cbtcwW->lpcs->lpszName))
195 csA.lpszName = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszName );
196 if (HIWORD(cbtcwW->lpcs->lpszClass))
197 csA.lpszClass = HEAP_strdupWtoA( GetProcessHeap(), 0, cbtcwW->lpcs->lpszClass );
198 ret = proc( code, wparam, (LPARAM)&cbtcwA );
199 cbtcwW->hwndInsertAfter = cbtcwA.hwndInsertAfter;
200 if (HIWORD(csA.lpszName)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszName );
201 if (HIWORD(csA.lpszClass)) HeapFree( GetProcessHeap(), 0, (LPSTR)csA.lpszClass );
207 /***********************************************************************
210 static LRESULT call_hook( HOOKPROC proc, INT id, INT code, WPARAM wparam, LPARAM lparam,
211 BOOL prev_unicode, BOOL next_unicode )
216 DPRINTF( "%08lx:Call hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx)\n",
217 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
219 if (!prev_unicode == !next_unicode) ret = proc( code, wparam, lparam );
220 else if (prev_unicode) ret = call_hook_WtoA( proc, id, code, wparam, lparam );
221 else ret = call_hook_AtoW( proc, id, code, wparam, lparam );
224 DPRINTF( "%08lx:Ret hook proc %p (id=%s,code=%x,wp=%08x,lp=%08lx) retval=%08lx\n",
225 GetCurrentThreadId(), proc, hook_names[id-WH_MINHOOK], code, wparam, lparam, ret );
231 /***********************************************************************
234 LRESULT HOOK_CallHooks( INT id, INT code, WPARAM wparam, LPARAM lparam, BOOL unicode )
236 MESSAGEQUEUE *queue = QUEUE_Current();
237 HOOKPROC proc = NULL;
239 BOOL unicode_hook = FALSE;
243 if (!queue) return 0;
245 SERVER_START_REQ( start_hook_chain )
248 if (!wine_server_call_err( req ))
250 queue->hook = reply->handle;
252 unicode_hook = reply->unicode;
259 TRACE( "calling hook %p %s code %x wp %x lp %lx\n",
260 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
262 locks = WIN_SuspendWndsLock();
263 ret = call_hook( proc, id, code, wparam, lparam, unicode, unicode_hook );
264 WIN_RestoreWndsLock( locks );
266 SERVER_START_REQ( finish_hook_chain )
269 wine_server_call( req );
279 /***********************************************************************
282 BOOL HOOK_IsHooked( INT id )
284 return TRUE; /* FIXME */
288 /***********************************************************************
289 * SetWindowsHookA (USER32.@)
291 HHOOK WINAPI SetWindowsHookA( INT id, HOOKPROC proc )
293 return SetWindowsHookExA( id, proc, 0, GetCurrentThreadId() );
297 /***********************************************************************
298 * SetWindowsHookW (USER32.@)
300 HHOOK WINAPI SetWindowsHookW( INT id, HOOKPROC proc )
302 return SetWindowsHookExW( id, proc, 0, GetCurrentThreadId() );
306 /***********************************************************************
307 * SetWindowsHookExA (USER32.@)
309 HHOOK WINAPI SetWindowsHookExA( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
311 return set_windows_hook( id, proc, inst, tid, FALSE );
314 /***********************************************************************
315 * SetWindowsHookExW (USER32.@)
317 HHOOK WINAPI SetWindowsHookExW( INT id, HOOKPROC proc, HINSTANCE inst, DWORD tid )
319 return set_windows_hook( id, proc, inst, tid, TRUE );
323 /***********************************************************************
324 * UnhookWindowsHook (USER32.@)
326 BOOL WINAPI UnhookWindowsHook( INT id, HOOKPROC proc )
330 TRACE( "%s %p\n", hook_names[id-WH_MINHOOK], proc );
332 SERVER_START_REQ( remove_hook )
336 ret = !wine_server_call_err( req );
344 /***********************************************************************
345 * UnhookWindowsHookEx (USER32.@)
347 BOOL WINAPI UnhookWindowsHookEx( HHOOK hhook )
351 TRACE( "%x\n", hhook );
353 SERVER_START_REQ( remove_hook )
356 ret = !wine_server_call_err( req );
363 /***********************************************************************
364 * CallNextHookEx (USER32.@)
366 LRESULT WINAPI CallNextHookEx( HHOOK hhook, INT code, WPARAM wparam, LPARAM lparam )
368 MESSAGEQUEUE *queue = QUEUE_Current();
370 HOOKPROC proc = NULL;
372 BOOL prev_unicode = FALSE, next_unicode = FALSE;
375 if (!queue) return 0;
377 SERVER_START_REQ( get_next_hook )
380 if (!wine_server_call_err( req ))
382 queue->hook = reply->next;
385 prev_unicode = reply->prev_unicode;
386 next_unicode = reply->next_unicode;
392 TRACE( "calling hook %p %s code %x wp %x lp %lx\n",
393 proc, hook_names[id-WH_MINHOOK], code, wparam, lparam );
395 return call_hook( proc, id, code, wparam, lparam, prev_unicode, next_unicode );
402 /***********************************************************************
403 * CallMsgFilterA (USER32.@)
405 BOOL WINAPI CallMsgFilterA( LPMSG msg, INT code )
407 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, FALSE )) return TRUE;
408 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, FALSE );
412 /***********************************************************************
413 * CallMsgFilterW (USER32.@)
415 BOOL WINAPI CallMsgFilterW( LPMSG msg, INT code )
417 if (HOOK_CallHooks( WH_SYSMSGFILTER, code, 0, (LPARAM)msg, TRUE )) return TRUE;
418 return HOOK_CallHooks( WH_MSGFILTER, code, 0, (LPARAM)msg, TRUE );
422 /***********************************************************************
423 * SetWinEventHook [USER32.@]
425 * Set up an event hook for a set of events.
428 * dwMin [I] Lowest event handled by pfnProc
429 * dwMax [I] Highest event handled by pfnProc
430 * hModule [I] DLL containing pfnProc
431 * pfnProc [I] Callback event hook function
432 * dwProcess [I] Process to get events from, or 0 for all processes
433 * dwThread [I] Thread to get events from, or 0 for all threads
434 * dwFlags [I] Flags indicating the status of pfnProc
437 * Success: A handle representing the hook.
438 * Failure: A NULL handle.
443 HWINEVENTHOOK WINAPI SetWinEventHook(DWORD dwMin, DWORD dwMax, HMODULE hModule,
444 WINEVENTPROC pfnProc, DWORD dwProcess,
445 DWORD dwThread, DWORD dwFlags)
447 FIXME("(%ld,%ld,0x%08x,%p,%ld,%ld,0x%08lx)-stub!\n", dwMin, dwMax, hModule,
448 pfnProc, dwProcess, dwThread, dwFlags);
453 /***********************************************************************
454 * UnhookWinEvent [USER32.@]
456 * Remove an event hook for a set of events.
459 * hEventHook [I] Event hook to remove
462 * Success: TRUE. The event hook has been removed.
463 * Failure: FALSE, if hEventHook is invalid.
468 BOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hEventHook)
470 FIXME("(%x)-stub!\n", hEventHook);
472 return (hEventHook != 0);
476 /***********************************************************************
477 * NotifyWinEvent [USER32.@]
479 * Inform the OS that an event has occurred.
482 * dwEvent [I] Id of the event
483 * hWnd [I] Window holding the object that created the event
484 * nId [I] Type of object that created the event
485 * nChildId [I] Child object of nId, or CHILDID_SELF.
493 void WINAPI NotifyWinEvent(DWORD dwEvent, HWND hWnd, LONG nId, LONG nChildId)
495 FIXME("(%ld,0x%08x,%ld,%ld)-stub!\n", dwEvent, hWnd, nId, nChildId);
499 /***********************************************************************
500 * IsWinEventHookInstalled [USER32.@]
502 * Determine if an event hook is installed for an event.
505 * dwEvent [I] Id of the event
508 * TRUE, If there are any hooks installed for the event.
514 BOOL WINAPI IsWinEventHookInstalled(DWORD dwEvent)
516 FIXME("(%ld)-stub!\n", dwEvent);