求助,hook了键盘后,异步操作容易报错CallbackOnCollectedDelegate

cciikk2 2017-01-18 10:14:53
不用异步不起新线程这么hook挺好的,但是起了新线程或者异步执行了啥,再动键盘就很容易出下面的错误,本人C#新手,虽然搜了半天知道是垃圾回收机制造成,用了网上常见的GC.KeepAlive()也没用,实在不知道从哪里下手,大家指点下吧

错误提示如下:

对“WindowsFormsApplication1!WindowsFormsApplication1.KBHook+LowLevelKeyboardProcDelegate::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。

调用hook的代码:

private void button4_Click(object sender, EventArgs e)
{
KBHook kbh = new KBHook();
string s = kbh.RunKBHook();
label1.Text = s;
}
private delegate void testtttt();
private void button2_Click(object sender, EventArgs e)
{

Control.CheckForIllegalCrossThreadCalls = false;
testtttt gogogo = new testtttt(DoWork);
gogogo.BeginInvoke(null, gogogo);
//Thread workerThread = new Thread(this.DoWork);
//workerThread.Start();
}
private void button7_Click(object sender, EventArgs e)
{
MessageBox.Show("Start!");
}
public void DoWork(){
WebServicesHelper wshelper = new WebServicesHelper();
string url = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx";
string mname = "getCountryCityByIp";
String[] args = new String[1];
args[0] = "111.178.11.70";
string namespaceurl = "WebXml.com.cn";
object o = wshelper.InvokeWebService(namespaceurl,url, mname, args);
string[] s = (string[])o;

textBox3.Text = s[1].ToString();
}



hook类

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;



namespace WindowsFormsApplication1
{
#region 禁用系统按键
public partial class KBHook
{
private struct KBDLLHOOKSTRUCT
{
public int vkCode;
int scanCode;
public int flags;
int time;
int dwExtraInfo;
}

private delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);

//禁用系统ctrl alt delete



//其他组合按键钩子
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId);

[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hHook);


[DllImport("user32.dll")]
private static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);

[DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(IntPtr path);

private IntPtr hHook;
LowLevelKeyboardProcDelegate hookProc; // prevent gc
const int WH_KEYBOARD_LL = 13;

public string RunKBHook()
{
string ret="";
// hook keyboard
IntPtr hModule = GetModuleHandle(IntPtr.Zero);
hookProc = new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hModule, 0);
if (hHook == IntPtr.Zero)
{
ret = "Failed to set hook, error = " + Marshal.GetLastWin32Error();
}else{
ret="ok";
}
return ret;
}

public void StopKBHook() {
UnhookWindowsHookEx(hHook); // release keyboard hook
}
//protected override void OnExit(ExitEventArgs e)
//{
// UnhookWindowsHookEx(hHook); // release keyboard hook
// base.OnExit(e);
//}

private static int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
{
if (nCode >= 0)
switch (wParam)
{
case 256: // WM_KEYDOWN
case 257: // WM_KEYUP
case 260: // WM_SYSKEYDOWN
case 261: // M_SYSKEYUP
if (
(lParam.vkCode == 0x09 && lParam.flags == 32) || // Alt+Tab
(lParam.vkCode == 0x1b && lParam.flags == 32) || // Alt+Esc
(lParam.vkCode == 0x73 && lParam.flags == 32) || // Alt+F4
(lParam.vkCode == 0x20 && lParam.flags == 32) || // Alt+Space
(lParam.vkCode == 0x1b && lParam.flags == 0) || // Ctrl+Esc

(lParam.vkCode == 0x2e && (((lParam.flags & 32) == 32) && (lParam.flags & 0) == 0)) || // Ctrl+Alt+Delete
(lParam.vkCode == 0x5b && lParam.flags == 1) || // Left Windows Key
(lParam.vkCode == 0x5c && lParam.flags == 1)) // Right Windows Key
{
return 1;
}
break;
}
return CallNextHookEx(0, nCode, wParam, ref lParam);
}
}
#endregion
}

...全文
174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jaye-L 2017-01-20
  • 打赏
  • 举报
回复
将回调函数存放在成员变量中,避免回调函数被GC销毁
笨啦啦 2017-01-19
  • 打赏
  • 举报
回复
线程间同步问题?你看看hook的对象怎么变化的?
cciikk2 2017-01-19
  • 打赏
  • 举报
回复
引用 2 楼 crystal_lz 的回复:
http://download.csdn.net/detail/crystal_lz/4557082
感谢您的代码,但是您这代码跟我的有类似问题,添加如下内容,在form加载时异步用去做点别的,比如杀任务管理器,虽然不报错,但hook也会失灵。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;
using System.Runtime.InteropServices;

namespace KeyHookTest
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern int SetWindowsHookEx(int idHook, HookProc hProc, IntPtr hMod, int dwThreadId);
        [DllImport("user32.dll")]
        public static extern int CallNextHookEx(int hHook, int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        public static extern bool UnhookWindowsHookEx(int hHook);
        [DllImport("kernel32.dll")]//获取模块句柄  
        public static extern IntPtr GetModuleHandle(string lpModuleName);


        private delegate void testtttt();


        private void KillTaskmgr()
        {
            while (true)
            {
                Process[] sum = Process.GetProcesses();
                foreach (Process p in sum)
                {
                    if (p.ProcessName == "taskmgr" || p.ProcessName == "cmd")
                    {
                        try
                        {
                            p.Kill();
                        }
                        catch { ;}
                    }
                }
            }
        }
        public struct KeyInfoStruct
        {
            public int vkCode;        //按键键码
            public int scanCode;
            public int flags;       //键盘是否按下的标志
            public int time;
            public int dwExtraInfo;
        }
        
        private const int WH_KEYBOARD_LL = 13;      //钩子类型 全局钩子
        private const int WM_KEYUP = 0x101;     //按键抬起
        private const int WM_KEYDOWN = 0x100;       //按键按下

        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        bool bStopMsg = false;
        int hHook = 0;
        GCHandle gc;

        public int MethodHookProc(int nCode, IntPtr wParam, IntPtr lParam) {
            if (nCode >= 0) {
                KeyInfoStruct inputInfo = (KeyInfoStruct)Marshal.PtrToStructure(lParam, typeof(KeyInfoStruct));
                if (wParam == (IntPtr)WM_KEYDOWN) {//如果按键按下
                    textBox1.Text += "-" + ((Keys)inputInfo.vkCode).ToString() + "-";
                }
                if (bStopMsg)
                    return 1;
            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);//继续传递消息
        }
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            button1.Text = "屏蔽按键";
            button1.Enabled = false;
            button2.Text = "设置Hook";


            testtttt gogogo = new testtttt(KillTaskmgr);
            gogogo.BeginInvoke(null, gogogo);
        }

        private void button1_Click(object sender, EventArgs e) {
            if (button1.Text == "屏蔽按键") {
                bStopMsg = true;
                button1.Text = "解除禁用";
            } else {
                bStopMsg = false;
                button1.Text = "屏蔽按键";
            }
        }

        private void button2_Click(object sender, EventArgs e) {
            if (0 == hHook) {
                HookProc KeyCallBack = new HookProc(MethodHookProc);
                hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyCallBack,
                    GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                if (hHook == 0) {
                    MessageBox.Show("设置Hook失败");
                } else {
                    button1.Enabled = true;
                    button2.Text = "卸载Hook";
                    gc = GCHandle.Alloc(KeyCallBack);   //保持活动 避免 回调过程 被垃圾回收
                }
            } else {
                if (UnhookWindowsHookEx(hHook)) {
                    hHook = 0;
                    button1.Enabled = false;
                    button2.Text = "设置Hook";
                    gc.Free();
                } else {
                    MessageBox.Show("卸载失败");
                }
            }
            MessageBox.Show(hHook.ToString());
        }

    }
}

110,530

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧