使用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. ZOJ-3278 8G Island---二分第k大

    题目链接: https://cn.vjudge.net/problem/ZOJ-3278 题目大意: 给出两个数列A和B,长度分别为N,M (1<=N, M<=10^5, 1<=Ai ...

  2. 远程调用内核接口的封装类(RCKObjs)

    RCK 包括 Application, Function, Connection, Command, Response 和 Fields 六 大类, 其主要功能例如以下:     a. Applica ...

  3. Discuz!在线中文分词服务

    Discuz!在线中文分词服务是基于API返回分词结果的.在项目中,我们只需要一个函数即可方便地进行分词.关键词提取.以下是根据Discuz!在线分词服务API写的函数,测试可正常运行: 代码代码如下 ...

  4. Go 测试单个方法

    1.目录 gotest.go package mytest import ( "errors" ) func Division(a, b float64) (float64, er ...

  5. 基于物理文件的HBase备份还原

    前提说明: 1.HBase数据分表,所以备份的粒度是表. 2.备份的内容为Azure的Blob存储. HBase Blob备份 备份时,需要先将表disable,以保持数据一致性. 备份的工具可以用A ...

  6. CS20Chapter2

    constants操作 import tensorflow as tf a = tf.constant([2, 2], name='a') b = tf.constant([[0, 1], [2, 3 ...

  7. POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  8. HDU 1165 公式推导题

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1165 Eddy's research II Time Limit: 4000/2000 MS (Java/Othe ...

  9. [原创]HBase学习笔记(2)- 基本操作

    1.使用hbase shell连接hbase 2.输入help可以查看帮助 3.输入list查看当前hbase中的所有表 4.使用create创建表test 其中test是表名,cf是列族.该表只创建 ...

  10. GCD 多线程技术

    Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用 的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的Dispatch Qu ...