标签:.net c# delegate hook钩子 应用程序
《对“XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们》的问题的解决方法/// <summary> /// 安装钩子的函数 /// </summary> /// <param name="idHook"></param> /// <param name="lpfn"></param> /// <param name="hInstance"></param> /// <param name="threadId"></param> /// <returns></returns> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); /// <summary> /// 卸下钩子的函数 /// </summary> /// <param name="idHook"></param> /// <returns></returns> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); /// <summary> /// 下一个钩挂的函数 /// </summary> /// <param name="idHook"></param> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); //定义了一个委托 public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); //定义一个代理 HookProc KeyboardHookDelegate; /// <summary> /// 创建钩子 /// </summary> public void SetHook() { KeyboardHookDelegate = new HookProc(KeyboardHookProc); Process cProcess = Process.GetCurrentProcess(); ProcessModule cModule = cProcess.MainModule; var mh = GetModuleHandle(cModule.ModuleName); hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookDelegate, mh, 0); } public void UnHook() { UnhookWindowsHookEx(hHook); } /// <summary> /// 键盘钩子进程 /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { //... //每到这里就报错 return CallNextHookEx(hHook, nCode, wParam, lParam); }
public void SetHook() { KeyboardHookDelegate = new HookProc(KeyboardHookProc); IntPtr intPtr = Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]); hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookDelegate, intPtr, 0); }
类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们的问题的解决方法
标签:.net c# delegate hook钩子 应用程序
原文地址:http://blog.csdn.net/chenhongwu666/article/details/42029965