using Microsoft.Win32;
public void KillCtrlAltDelete() { RegistryKey regkey; string keyValueInt = "1"; 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()); } }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; base.OnClosing(e); }
The LowLevelKeyboardProc procedure is a program-defined or library-specific function call procedure used in conjunction with the SetWindowsHookEx function. The system calls this function each time a keyboard input event is about to be placed in the input stream queue. Keyboard input can pass through a driver or through calls to the keybdevent function. If the call came through a keybdevent call, then it was “injected”. However, the WHKEYBOARDLL hook is not embedded in another process. Instead, the context switches to another process on which the hook is installed and is called in its original form. Then, the context switches back to the application that triggered the event.
using System.Runtime.InteropServices; using System.Security.Principal; using System.Diagnostics;
[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 = 13; /*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 = 0; private const int SW_SHOW = 1; 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 256: case 257: case 260: case 261: //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key, blnEat = ((lParam.vkCode == 9) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 0)) | ((lParam.vkCode == 91) && (lParam.flags == 1)) | ((lParam.vkCode == 92) && (lParam.flags == 1)) | ((lParam.vkCode == 73) && (lParam.flags == 0)); break; } if (blnEat == true) { return 1; } else { return CallNextHookEx(0, 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()[0]).ToInt32(), 0); }
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); }
intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0);
var inst = LoadLibrary("user32.dll").ToInt32(); intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, inst, 0);
LoadLibrary: [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] public static extern IntPtr LoadLibrary(string lpFileName);
Source: https://habr.com/ru/post/206480/
All Articles