To intercept messages of all applications in the system, you must write the hook function in the dynamic connection library. Otherwise, the hook can only intercept messages of the application. In fact, this principle is very simple. Let's look at the following API for installing the HOOK:
Hhook setwindowshookex ( IntIdhook,// Type of hook to install
HookprocLpfn,// Address of hook procedure
HinstanceHmod,// Handle to application instance
DWORDDwthreadid// Identity of thread to install hook
); Note that the third parameter (
Hinstance hmod) And the fourth parameter (
DWORD dwthreadidThe two parameters indicate a DLL instance handle and the thread ID to intercept messages. If hmod is null, dwthreadid must be specified. If dwthreadid is 0, the hook is valid for all threads, but you must specify hmod. This is because a DLL is an independent thread, it is independent of other applications and can intercept messages from other threads. The following function implements hook installation:
//////////////////////////////////////// ///////////////////////////////////// // The keyboard hook install function implementDllexport void winapi installhook () { G_keyboardhook = setwindowshookex (wh_keyboard, (hookproc) keyboardhookproc, Theapp. m_hinstance, 0 ); G_mousehook = setwindowshookex (wh_mouse, (hookproc) mousehookproc, Theapp. m_hinstance, 0 ); }Theapp is a global object of the DLL class.The following function uninstalls the HOOK:
//////////////////////////////////////// ///////////////////////////////////// // The keyboard hook uninstall function implementDllexport void winapi uninstallhook () { Unhookwindowshookex (g_keyboardhook ); Unhookwindowshookex (g_mousehook ); }The following two functions are hook functions for the keyboard and mouse:
//////////////////////////////////////// ///////////////////////////////////// // The keyboard & mouse hook function implementDllexport lresult callback keyboardhookproc (INT ncode, wparam, lparam) { If (ncode = hc_action) { Switch (wparam) { Case vk_space: If (getkeystate (vk_lcontrol) & 0x8000 )! = 0) & (getkeystate (vk_rshift) & 0x8000 )! = 0 )) { Beatkeystroke = false; Beatmousestroke = false; } Break; } }Return (beatkeystroke? 1: callnexthookex (g_keyboardhook, ncode, wparam, lparam )); }Dllexport lresult callback mousehookproc (INT ncode, wparam, lparam) { Return (beatmousestroke? 1: callnexthookex (g_mousehook, ncode, wparam, lparam )); }It is very simple. Here we only judge whether the left Ctrl + Right Shift + space is pressed. If it is, it will be unlocked. Finally, introduce the DLL Lib in the application, and call the above function to install the hook in the appropriate position of the application, and then uninstall it. The following is an application that can lock the keyboard and mouse for a period of time:
// Timerlock. h // Head File // Declare application class and window class // Lionmountainman // 2005-8-25Class cmyapp: Public cwinapp { Protected: Ctime m_time; Public: Virtual bool initinstance (); Virtual int run (); };//////////////////////////////////////// //////////////////////////////////////// ///// // Timerlock. cpp // Implement File // Implement the timer LOCK keyboard and mouse function. // Lionmountainman // 2005-8-25# Include <afxwin. h> # Include "timerlock. H" # Include "hookdll. H"Cmyapp MyApp;//////////////////////////////////////// //////////////////////////////////////// ///// // Cmyapp member functionsBool cmyapp: initinstance () { M_time = ctime: getcurrenttime (); Return true; }Int cmyapp: Run () { Bool block = false; Ctime tmalarm1 (m_time.getyear (), m_time.getmonth (),/ M_time.getday (), 10, 30, 0 ); Ctime tmcancelalarm1 (m_time.getyear (), m_time.getmonth (),/ M_time.getday (), 10, 40, 0 ); Ctime tmalarm2 (m_time.getyear (), m_time.getmonth (),/ M_time.getday (), 15, 30, 0 ); Ctime tmcancelalarm2 (m_time.getyear (), m_time.getmonth (),/ M_time.getday (), 15, 40, 0 ); Ctime tmalarm3 (m_time.getyear (), m_time.getmonth (),/ M_time.getday (), 12, 30, 0 ); Ctime tmcancelalarm3 (m_time.getyear (), m_time.getmonth (), M_time.getday (), 13, 30, 0 ); Ctime tmalarm4 (m_time.getyear (), m_time.getmonth (),/ M_time.getday (), 17,30, 0 ); Ctime tmcancelalarm4 (m_time.getyear (), m_time.getmonth (),/ M_time.getday (), 18, 0, 0 );
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.