相关文章推荐
呐喊的竹笋  ·  Environment.GetExterna ...·  2 月前    · 
酒量大的钥匙  ·  sqlalchemy.exc.operati ...·  9 月前    · 
活泼的芒果  ·  java返回excel文件流-掘金·  1 年前    · 
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

Getting the error , A callback was made on a garbage collected delegate of type Form1+LowLevelKeyboardProcDelegate::Invoke'

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.

it crashes only when the Timer1 event is invoked , if the timer code is removed the rest of the app is working well, I am wondering why the application is crashing because of these ? – Chikku Jacob Jun 14, 2017 at 13:58 I cant use it because this can be invoked by user at certain time, at the same time he might be running other apps. All I want is freeze inputs while my app runs, if the user press ctrl alt del and select task manager , it shouldn't be allowed. I don't want to disable task manager by editing Registry, but rather kill the Task manager process. – Chikku Jacob Jun 14, 2017 at 14:01 Dude, forget this approach, use something like dynamically locking the session or something else, I've been here and I can tell you it's impossible to effectively block input with an application, the user will always find a way to kill your app and execute whatever application wants, through a console, a program which executes an script, a batch file, swapping sessions or any other method. – Gusman Jun 14, 2017 at 14:10

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.