C# Disable CTRL-ALT-DEL, ALT-TAB, ALT-F4, Start Menu and so on…
使用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…的更多相关文章
- C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入
原文:C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入 Win32.cs /* * * FileCreate By Bluefire * Used To Import WindowsApi ...
- windows 中去除Ctrl+Alt+Del才能登录
安装windows 7后登录的时候有一样很麻烦的步骤是需要先按Ctrl+Alt+Del,才能输入用户密码进行登录.这里笔者介绍一下如何取消这个东西. 点击“开始菜单”,点击“控制面板”. [管理工具] ...
- Widows2003开机取消按CTRL+ALT+DEL
一, Widows2003开机取消按CTRL+ALT+DEL 1. 单击windows开始键→管理工具→本地安全策略(如下图) 2. 本地安全设置→本地策略→安全选项 3. 安全选项→右侧→找到这个文 ...
- 远程桌面如何向远程的计算机发送ctrl+alt+del
远程桌面如何向远程的计算机发送ctrl+alt+del ? 可以使用 ctrl+alt+end 组合键代替 ctrl+alt+del 组合键
- [OS] 如何在远程机器上用ctrl+alt+del键修改登录用户的密码
远程登录某台机器,想修改当前登录用户的密码,系统提示按Ctrl+Alt+Del,在出现的界面里修改密码 但我一按这三个键,是在我本地客户机生效,而不是在远程服务器. 答案 : 向远程桌面发送Ctrl+ ...
- 任务管理器 用 Ctrl + Shift + Esc 替换 Ctrl + Alt + Del
任务管理器 用 Ctrl + Shift + Esc 替换 Ctrl + Alt + Del
- centos7 取消Ctrl+Alt+Del重启功能
转载:http://www.cnblogs.com/huangjc/p/4536620.html Linux默认允许任何人按下Ctrl+Alt+Del来重启系统.但是在生产环境中,应该停用按下Ctrl ...
- Ring3下无驱动移除winlogon.exe进程ctrl+alt+del,win+u,win+l三个系统热键,非屏蔽热键(子类化SAS 窗口)
随手而作,纯粹技术研究,没什么实际意义. 打开xuetr,正常情况下.winlogon.exe注册了三个热键.ctrl+alt+del,win+u,win+l三个. 这三个键用SetWindowsHo ...
- CentOS版本禁用Ctrl+Alt+Del重启功能
1 禁用Ctrl+Alt+Del重启功能(不重启系统的前提条件) 1.1 CentOS 6 ##查看/etc/inittab确认Ctrl+Alt+Del相关配置文件 cat /etc/initta ...
- Windows Server2003 关闭 关机信息、开机ctrl+alt+del
取消CTRL+ALT+DEL win+R 或从"开始"打开"运行",输入gpedit.msc打开"组策略编辑器",依次展开"计算机 ...
随机推荐
- jq复制到剪切板插件clipboard.min.js(兼容IE9)
/*! * clipboard.js v1.5.5 * https://zenorocha.github.io/clipboard.js * * Licensed MIT 漏 Zeno Rocha * ...
- ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集
题目链接: https://cn.vjudge.net/problem/ZOJ-3261 题目大意: 给你一些点,还有一些边,每个点上都有一个权值,然后有一些询问,分为两种,query a 询问与a直 ...
- 随手练——HDU-2037 、P-2920 时间安排(贪心)
普通时间安排 HDU-2037 :http://acm.hdu.edu.cn/showproblem.php?pid=2037 选取结束时间早的策略. #include <iostream> ...
- 【CSS】关于flex
flex 属性用于设置或检索弹性盒模型对象的子元素如何分配空间. 如果元素不是弹性盒模型对象的子元素,则 flex 属性不起作用. 设为Flex布局以后,子元素的float.clear和vertica ...
- 7、Web Service-IDEA-jaxws规范下的 服务端/客户端 开发
前提简介:这里之后即使基于IDEA进行开发的,风格与之前有些不同之处! 1.服务端的开发 1.创建新的项目 2.pom.xml 添加开发时所需要的依赖 <?xml version="1 ...
- Css3 实现丝带效果
代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- [LuoguP1360][USACP07MAR]黄金阵容均衡
[LuoguP1360][USACP07MAR]黄金阵容均衡(Link) 每天会增加一个数\(A\),将\(A\)二进制分解为\(a[i]\),对于每一个\(i\)都增加\(a[i]\),如果一段时间 ...
- Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】
Spring Cloud(四):服务容错保护 Hystrix[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 分布式系统中经常会出现某个基础服务不可用 ...
- React Native封装Toast与加载Loading组件
React Native开发封装Toast与加载Loading组件 在App开发中,我们避免不了使用的两个组件,一个Toast,一个网络加载Loading,在RN开发中,也是一样,React Nati ...
- js中的冒泡排序
<!-- 冒泡排序:把一组数列按照一定的顺序进行排列,从大到小,或者从小到大 --> // 控制循环的轮数 arr = [3,14,55,2,1,4,5 ...