If you integrate a system based on a Windows operating system, you may want your final product to exclusively occupy system resources. You want to regulate user behavior. For example, you do not want the user to terminate a process by pressing CTRL + ALT + DEL, or press win to bring up the Start Menu,
Or press Alt + TAB to switch to another application.Program. I already have a related articleArticleIn response to blocking CTRL + ALT + DEL under Win2k/NT, this article describes how to block the response of CTRL + ALT + DEL through the Gina programming interface. As a continuation, this article will continue to introduce how to block the combination of win key and ALT + Tab key.
Since the response of these buttons is system-level, we cannot simply control them through a program. Therefore, we need to use another programming interface, Hook, provided by Microsoft ). You may already know about hooks (there are many articles on Hook Technology and Applications on the Internet ). To put it simply, a hook is a technique that intercepts a specific event (Message) by replacing the standard interface provided by the system to change or enhance the default behavior of the system. Our current task is to intercept the user before pressing win or Alt + TAB, but the system has not responded to them, and then change the default behavior of the system. Obviously, we need to create a global hook (the hook function is implemented in a separate DLL) and a low-level keyboard hook ).
Step 1: Implement hook DLL. First, we need to define a global data area (remember this is a global hook), as shown in the following (put in the header of the CPP file ):
# Pragma data_seg ("mydata ")
Hhook glhhook = NULL; // installed mouse hook handle
Hinstance glhinstance = NULL; // DLL instance handle
# Pragma data_seg ()
Then declare the data zone in the. Def file as follows:
Sections
Mydata read write shared
When this DLL is loaded by a process, the program enters from winmain. At this time, we need to save the module handle as follows:
Glhinstance = (hinstance) hmodule;
Next, we need to define two export functions and hook processing functions. Let's take a closer look at this hook processing function (the other two export functions are relatively simple. You just need to call setwindowshookex and unhookwindowshookex to install/uninstall the hook function. Pay attention that the first parameter of setwindowshookex is wh_keyboard_ll, the fourth parameter is 0 ).
// Low-level keyboard hook handling function
Lresult callback lowlevelkeyboardproc (INT ncode, wparam, lparam)
{
Bool featkeystroke = false;
Pkbdllhookstruct P = NULL;
If (ncode = hc_action)
{
P = (pkbdllhookstruct) lparam;
Switch (wparam)
{
Case wm_keydown:
Case wm_syskeydown:
Case wm_keyup:
Case wm_syskeyup:
Featkeystroke = (p-> vkcode = vk_lwin) & brvbar; (p-> vkcode = vk_rwin) & brvbar; // shield win
// Mask Alt + Tab
(P-> vkcode = vk_tab) & (P-> flags & llkhf_altdown )! = 0) & brvbar;
// Block Alt + ESC
(P-> vkcode = vk_escape) & (p-> flags & llkhf_altdown )! = 0) & brvbar;
// Block Ctrl + ESC
(P-> vkcode = vk_escape) & (getkeystate (vk_control) & 0x8000 )! = 0 ));
Break;
Default:
Break;
}
}
Return (featkeystroke? True: callnexthookex (glhhook, ncode, wparam, lparam ));
}
As you can see, when the program finds that the press is the win key or Alt + Tab key combination, it will no longer call the callnexthookex function to pass the message. In this way, we have blocked the response of these buttons.
Step 2: Test the program of hook DLL. Create a dialog box-based application in VC. By calling loadlibrary ("keymask. DLL ") load the hook DLL, and use getprocaddress (m_hdll," startkeymask ") and getprocaddress (m_hdll," stopkeymask ") to import two functions to install/uninstall the hook. In the main dialog box, define two buttons to call these two functions respectively, as shown below:
When you press the "start_hook" button, our hook function works. Try the win key or the Alt + Tab key combination. Isn't it done ?! The "stop_hook" button can remove this hook.
At this point, you may think that hooks are actually very easy. Yes, hooks are easy to use and powerful. However, I suggest using as few hooks as possible if not necessary. This is because the hooks may seriously reduce the performance of your system while implementing powerful functions. Sometimes it is not worth the candle!