使用C#禁用系统的某些特定按键

原文地址:http://www.tamas.io/c-disable-ctrl-alt-del-alt-tab-alt-f4-start-menu-and-so-on/


I wrote an article back in 2007 that is still around the web, in various discussions including StackOverflow and DotNetSpider, so I have decided to re-publish the article. This article was relevant back then - and it wasn't tested on a Windows 7 environment purely due to the fact that it did not exist and please also note that comments are disabled for this post.

Before continue reading please note that this article doesn’t intend to call upon you to create nasty applications. If you use this code it should be for your own fun or for learning purposes.

After doing some research on disabling keys or key combinations I found out that there are several ways of achieving the before mentioned key combos. CTRL-ALT-DEL combo is part of the SAS (Secure Attention Sequence) thus the solution to disable this is to write your own gina.dll (Graphical Identification and Authentication).

Don’t worry, I’m not looking into that as for now, I’m going to show you the work around. We will use C#’s Registry editing possibilities to set/change the group policy for the CTRL-ALT-DEL key sequence. Let’s see what we are about to do without programming anything. Open Start Menu > Run and enter gpedig.msc. Navigate to: User Configuration > Administrative Templates > System > CTRL+ALT+DELETEOptions.This is the place where you normally set the behaviour of the key combo. Select Remove Task Manager > Double-click the Remove Task Manager option.

If you change it’s value, the following registry entry gets created/modified:Software\Microsoft\Windows\CurrentVersion\Policies\System and the value of DisableTaskMgr gets set to 1.

Now, the task is set. Let’s get down to business and start coding:

Important thing, don’t miss out this line:

using Microsoft.Win32;

And now the method I have created looks like this:

    public void KillCtrlAltDelete()
{
RegistryKey regkey;
string keyValueInt = "";
string subKey = "Software\Microsoft\Windows\CurrentVersion\Policies\System"; try
{
regkey = Registry.CurrentUser.CreateSubKey(subKey);
regkey.SetValue("DisableTaskMgr", keyValueInt);
regkey.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

So the CTRL-ALT-DEL combo has been taken care of, let’s see the rest. You might have found this a bit difficult, so here’s an easy one. How to disable ALT+F4. 5 lines of code alltogether:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
base.OnClosing(e);
}

Okay. As for the rest I was reading many many articles, and they gave a lot of help. I can’t name one, as I was looking into at least 15 which all held some useful peace of information. I will give you the the source of the method called hooks. The code snippet uses the LowLevelKeyboardProc which is:

The LowLevelKeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function every time a new keyboard input event is about to be posted into a thread input queue. The keyboard input can come from the local keyboard driver or from calls to the keybdevent function. If the input comes from a call to keybdevent, the input was “injected”. However, the WHKEYBOARDLL hook is not injected into another process. Instead, the context switches back to the process that installed the hook and it is called in its original context. Then the context switches back to the application that generated the event.

Again, dont forget:

using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Diagnostics;

Here’s the rest what you need:

    [DllImport("user32", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, int hMod, int dwThreadId);
[DllImport("user32", EntryPoint = "UnhookWindowsHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int UnhookWindowsHookEx(int hHook);
public delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
[DllImport("user32", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
public const int WH_KEYBOARD_LL = ; /*code needed to disable start menu*/
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command); private const int SW_HIDE = ;
private const int SW_SHOW = ;
public struct KBDLLHOOKSTRUCT
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
public static int intLLKey; public int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
{
bool blnEat = false; switch (wParam)
{
case :
case :
case :
case :
//Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key,
blnEat = ((lParam.vkCode == ) && (lParam.flags == )) | ((lParam.vkCode == ) && (lParam.flags == )) | ((lParam.vkCode == ) && (lParam.flags == )) | ((lParam.vkCode == ) && (lParam.flags == )) | ((lParam.vkCode == ) && (lParam.flags == )) | ((lParam.vkCode == ) && (lParam.flags == ));
break;
} if (blnEat == true)
{
return ;
}
else
{
return CallNextHookEx(, nCode, wParam, ref lParam);
}
}
public void KillStartMenu()
{
int hwnd = FindWindow("Shell_TrayWnd", "");
ShowWindow(hwnd, SW_HIDE);
}
private void Form1_Load(object sender, EventArgs e)
{
intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[]).ToInt32(), );
}

Quite obviously you can programmatically reset these values. Here’s the code to re-enable everything:

    public static void ShowStartMenu()
{
int hwnd = FindWindow("Shell_TrayWnd", "");
ShowWindow(hwnd, SW_SHOW);
}
public static void EnableCTRLALTDEL()
{
try
{
string subKey = "Software\Microsoft\Windows\CurrentVersion\Policies\System";
RegistryKey rk = Registry.CurrentUser;
RegistryKey sk1 = rk.OpenSubKey(subKey);
if (sk1 != null)
rk.DeleteSubKeyTree(subKey);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnhookWindowsHookEx(intLLKey);
}

I hope you enjoyed my article and that you found some useful bits. I tried to collect all the information I managed to find during my research on this topic.

C# Disable CTRL-ALT-DEL, ALT-TAB, ALT-F4, Start Menu and so on…的更多相关文章

  1. C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入

    原文:C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入 Win32.cs /* * * FileCreate By Bluefire * Used To Import WindowsApi ...

  2. windows 中去除Ctrl+Alt+Del才能登录

    安装windows 7后登录的时候有一样很麻烦的步骤是需要先按Ctrl+Alt+Del,才能输入用户密码进行登录.这里笔者介绍一下如何取消这个东西. 点击“开始菜单”,点击“控制面板”. [管理工具] ...

  3. Widows2003开机取消按CTRL+ALT+DEL

    一, Widows2003开机取消按CTRL+ALT+DEL 1. 单击windows开始键→管理工具→本地安全策略(如下图) 2. 本地安全设置→本地策略→安全选项 3. 安全选项→右侧→找到这个文 ...

  4. 远程桌面如何向远程的计算机发送ctrl+alt+del

    远程桌面如何向远程的计算机发送ctrl+alt+del ? 可以使用 ctrl+alt+end 组合键代替 ctrl+alt+del 组合键

  5. [OS] 如何在远程机器上用ctrl+alt+del键修改登录用户的密码

    远程登录某台机器,想修改当前登录用户的密码,系统提示按Ctrl+Alt+Del,在出现的界面里修改密码 但我一按这三个键,是在我本地客户机生效,而不是在远程服务器. 答案 : 向远程桌面发送Ctrl+ ...

  6. 任务管理器 用 Ctrl + Shift + Esc 替换 Ctrl + Alt + Del

    任务管理器 用 Ctrl + Shift + Esc 替换 Ctrl + Alt + Del

  7. centos7 取消Ctrl+Alt+Del重启功能

    转载:http://www.cnblogs.com/huangjc/p/4536620.html Linux默认允许任何人按下Ctrl+Alt+Del来重启系统.但是在生产环境中,应该停用按下Ctrl ...

  8. Ring3下无驱动移除winlogon.exe进程ctrl+alt+del,win+u,win+l三个系统热键,非屏蔽热键(子类化SAS 窗口)

    随手而作,纯粹技术研究,没什么实际意义. 打开xuetr,正常情况下.winlogon.exe注册了三个热键.ctrl+alt+del,win+u,win+l三个. 这三个键用SetWindowsHo ...

  9. CentOS版本禁用Ctrl+Alt+Del重启功能

    1  禁用Ctrl+Alt+Del重启功能(不重启系统的前提条件) 1.1  CentOS 6 ##查看/etc/inittab确认Ctrl+Alt+Del相关配置文件 cat /etc/initta ...

  10. Windows Server2003 关闭 关机信息、开机ctrl+alt+del

    取消CTRL+ALT+DEL win+R 或从"开始"打开"运行",输入gpedit.msc打开"组策略编辑器",依次展开"计算机 ...

随机推荐

  1. 10、Node.js模块系统

    ##################################################################################介绍Node.js模块系统为了让No ...

  2. Running Protractor Tests on Docker

    配置这个Protractor环境真是折磨死人了,webdriver-manage update怎么都不成功,即使自己下载好chromederiver放到相应文件夹下,也不能使用.费时三四天终于按照ht ...

  3. 【[HNOI2015]亚瑟王】

    神仙题,抄题解 用\(tp_i\)表示\(i\)这个技能在\(r\)轮中被使用过的概率 于是最后的答案就是\(\sum_{i=1}^nd_i*tp_i\) 首先\(tp_1=1-(1-p_1)^r\) ...

  4. 【vue】todolist小练习

    参考链接: http://www.imooc.com/learn/694 http://www.cnblogs.com/Chen-XiaoJun/p/6238137.html http://blog. ...

  5. 26、springboot与消息

    概述: 1.大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力消息服务中两个重要概念:           2.消息代理(message broker)和目的地(destination) ...

  6. java的四舍五入

    四舍五入是我们小学的数学问题,这个问题对于我们程序猿来说就类似于1到10的加减乘除那么简单了.在讲解之间我们先看如下一个经典的案例: public static void main(String[] ...

  7. JDK(三)JDK1.8源码分析【排序】mergeSort

    归并排序是JDK对象数组的排序算法之一,接下来开始分析JDK的归并排序算法. /** * 将指定范围的对象数组按自然顺序升序排序 * * Src is the source array that st ...

  8. 我的QT5学习之路(四)——信号槽

    一.前言 前面说了Qt最基本的实例创建.控件以及工具集的介绍,相当于对于Qt有了一个初次的认识,这次我们开始认识Qt信号通信的重点之一——信号槽. 二.信号槽 信号槽是 Qt 框架引以为豪的机制之一. ...

  9. 使用JedisCluster出现异常:java.lang.NumberFormatException

    在使用JedisCluster进行测试时出现如下异常: java.lang.NumberFormatException: For input string: "7004@17004" ...

  10. spring cloud gateway 之限流篇

    转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在高并发的系统中,往往需要在系统中做限流,一方面是为了防止大量的请求使服务器过载,导致服务不可用,另一方 ...