Вы находитесь на странице: 1из 2

Using Hooks

The following code examples demonstrate how to perform the following tasks assoc iated with hooks: Installing and Releasing Hook Procedures Monitoring System Events Installing and Releasing Hook Procedures You can install a hook procedure by calling the SetWindowsHookEx function and sp ecifying the type of hook calling the procedure, whether the procedure should be associated with all threads in the same desktop as the calling thread or with a particular thread, and a pointer to the procedure entry point. You must place a global hook procedure in a DLL separate from the application in stalling the hook procedure. The installing application must have the handle to the DLL module before it can install the hook procedure. To retrieve a handle to the DLL module, call the LoadLibrary function with the name of the DLL. After y ou have obtained the handle, you can call the GetProcAddress function to retriev e a pointer to the hook procedure. Finally, use SetWindowsHookEx to install the hook procedure address in the appropriate hook chain. SetWindowsHookEx passes th e module handle, a pointer to the hook-procedure entry point, and 0 for the thre ad identifier, indicating that the hook procedure should be associated with all threads in the same desktop as the calling thread. This sequence is shown in the following example.

Copy

HOOKPROC hkprcSysMsg; static HINSTANCE hinstDLL; static HHOOK hhookSysMsg; hinstDLL = LoadLibrary(TEXT("c:\\myapp\\sysmsg.dll")); hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "SysMessageProc"); hhookSysMsg = SetWindowsHookEx( WH_SYSMSGFILTER, hkprcSysMsg, hinstDLL, 0); You can release a thread-specific hook procedure (remove its address from the ho ok chain) by calling the UnhookWindowsHookEx function, specifying the handle to the hook procedure to release. Release a hook procedure as soon as your applicat ion no longer needs it.

You can release a global hook procedure by using UnhookWindowsHookEx, but this f unction does not free the DLL containing the hook procedure. This is because glo bal hook procedures are called in the process context of every application in th e desktop, causing an implicit call to the LoadLibrary function for all of those processes. Because a call to the FreeLibrary function cannot be made for anothe r process, there is then no way to free the DLL. The system eventually frees the DLL after all processes explicitly linked to the DLL have either terminated or called FreeLibrary and all processes that called the hook procedure have resumed processing outside the DLL. An alternative method for installing a global hook procedure is to provide an in stallation function in the DLL, along with the hook procedure. With this method, the installing application does not need the handle to the DLL module. By linki ng with the DLL, the application gains access to the installation function. The installation function can supply the DLL module handle and other details in the call to SetWindowsHookEx. The DLL can also contain a function that releases the global hook procedure; the application can call this hook-releasing function whe n terminating. Monitoring System Events The following example uses a variety of thread-specific hook procedures to monit or the system for events affecting a thread. It demonstrates how to process even ts for the following types of hook procedures: WH_CALLWNDPROC WH_CBT WH_DEBUG WH_GETMESSAGE WH_KEYBOARD WH_MOUSE WH_MSGFILTER The user can procedure is he procedure ation's main install and remove a hook procedure by using the menu. When a hook installed and an event that is monitored by the procedure occurs, t writes information about the event to the client area of the applic window.

Вам также может понравиться