当前位置:编程学习 > C#/ASP.NET >>

C# 运用钩子做虚拟键盘程序,程序崩溃,提示:向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。

我引用的DLL是网上找的一个,叫CnBlogs.Youzai.HookEx.dll
提示出错的地方是:

private IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) {
            // if ok and someone listens to our events
            EventHandler<MouseExEventArgs> handler = this.Events[EventMouseActivity] as EventHandler<MouseExEventArgs>;
            if ((nCode >= 0) && (handler != null)) {
                //Marshall the data from callback.
                MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));

                //detect button clicked
                MouseButtons button = MouseButtons.None;
                short mouseDelta = 0;
                switch ((int)wParam) {
                case WM_LBUTTONDOWN:
                    // case WM_LBUTTONUP: 
                    // case WM_LBUTTONDBLCLK: 
                    button = MouseButtons.Left;
                    break;
                case WM_RBUTTONDOWN:
                    // case WM_RBUTTONUP: 
                    // case WM_RBUTTONDBLCLK: 
                    button = MouseButtons.Right;
                    break;
                case WM_MOUSEWHEEL:
                    // If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta. 
                    // One wheel click is defined as WHEEL_DELTA, which is 120. 
                    // (value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                    mouseDelta = unchecked((short)((mouseHookStruct.mouseData >> 16) & 0xffff));
                    //TODO: X BUTTONS (I havent them so was unable to test)
                    //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, 
                    //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, 
                    //and the low-order word is reserved. This value can be one or more of the following values. 
                    //Otherwise, mouseData is not used. 
                    break;
                }

                //double clicks
                int clickCount = 0;
                if (button != MouseButtons.None)
                    if ((int)wParam == WM_LBUTTONDBLCLK || (int)wParam == WM_RBUTTONDBLCLK)
                        clickCount = 2;
                    else
                        clickCount = 1;

                //generate event 
                MouseExEventArgs e = new MouseExEventArgs(
                   button,
                   clickCount,
                   mouseHookStruct.pt.x,
                   mouseHookStruct.pt.y,
                   mouseDelta,
                   mouseHookStruct.flags
                );

                //raise it
                handler(this, e);
            }
            //call next hook;
            return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
        }

错误信息如下:

对“CnBlogs.Youzai.HookEx!CnBlogs.Youzai.HookEx.UserActivityHook+HookProc::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。 虚拟键盘 钩子 委托 c# --------------------编程问答-------------------- 为什么要使用别人的dll啊 
自己写个钩子啊
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,