得到错误,回调是在Form1 + LowLevelKeyboardProcDelegate :: Invoke'类型的垃圾收集委托上进行的

时间:2017-06-14 13:55:31

标签: c# keyboard hook

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        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();
                }

//调用锁定密钥的方法

                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);
                    }
                }

//关闭开始菜单或Windows键

                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();            
                }


            }
        }

我正在开发一个无法通过用户交互关闭的应用程序。

我正在尝试禁用Windows键,任务管理器等应用程序工作正常但是当我点击ctrl alt del并且焦点从锁定屏幕返回到form1时它崩溃并出现以下错误,对垃圾进行了回调收集了'TrialLocks!TrialLocks.Form1 + LowLevelKeyboardProcDelegate :: Invoke'类型的委托

仅当我尝试关闭任务管理器时,应用程序才会抛出错误。我认为问题与此处相同说明中的其他问题有所不同。

任何指导帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

为LowLevelKeyboardProc创建变量,否则将被处理

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);            
    }
    ...
}