Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Diagnostics;
// The application can disable windows key task manager and ctrl esc etc
namespace TrialLocks
public partial class Form1 : Form
[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;
// Initialize
public Form1()
InitializeComponent();
// Method to invoke lock keys
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;
return CallNextHookEx(0, nCode, wParam, ref lParam);
// Close start menu or windows key
public void KillStartMenu()
int hwnd = FindWindow("Shell_TrayWnd", "");
ShowWindow(hwnd, SW_HIDE);
// call the form load event and start key board hook
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);
// closing the main form
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
UnhookWindowsHookEx(intLLKey);
private void timer1_Tick(object sender, EventArgs e)
foreach (Process selProcess in Process.GetProcesses())
if (selProcess.ProcessName == "taskmgr")
selProcess.Kill();
// Timer getting started
private void Form1_Activated(object sender, EventArgs e)
timer1.Start();
// Timer getting stopped
private void Form1_Deactivate(object sender, EventArgs e)
timer1.Stop();
I am Developing an application which cannot be closed by the user interaction.
I am Trying to disable windows keys, task manager etc the application is working fine but when I click on ctrl alt del and focus comes back from lock screen to form1 it crashes with the following error, A callback was made on a garbage collected delegate of type 'TrialLocks!TrialLocks.Form1+LowLevelKeyboardProcDelegate::Invoke'
The application throws error only when I try to close the Task manager. I think that's where the question is different from the other questions on the same note here.
Any help of guidance would be great.
–
–
–
Create variable for LowLevelKeyboardProc, otherwise it will be disposed
public partial class Form1 : Form
LowLevelKeyboardProcDelegate del;
private void Form1_Load(object sender, EventArgs e)
del = new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc);
intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, del, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.