MouseHook stops sending input randomaly

Viewed 17

I have a MouseHook class that I got, And I created an event with it, but it's randomly stopping sending input to the event for some reason, Its sending the location/buttons for a couple of seconds to minutes but for some reason it just stops randomly

I created and event like that

MouseHook mouse = new MouseHook();
mouse.OnMouseActivity += new System.Windows.Forms.MouseEventHandler(Mouse_OnMouseActivity);
mouse.Start();

And that's how I notice (For some reasong after couple of seconds-minutes it just stops sending input)

 void Mouse_OnMouseActivity(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        num++;
        Console.WriteLine(num);


        
    }

and that's the class:

public class MouseHook
{
    private const int WM_MOUSEMOVE = 0x200;
    private const int WM_LBUTTONDOWN = 0x201;
    private const int WM_RBUTTONDOWN = 0x204;
    private const int WM_MBUTTONDOWN = 0x207;
    private const int WM_LBUTTONUP = 0x202;
    private const int WM_RBUTTONUP = 0x205;
    private const int WM_MBUTTONUP = 0x208;
    private const int WM_LBUTTONDBLCLK = 0x203;
    private const int WM_RBUTTONDBLCLK = 0x206;
    private const int WM_MBUTTONDBLCLK = 0x209;


    public event System.Windows.Forms.MouseEventHandler OnMouseActivity;

    static int hMouseHook = 0;


    public const int WH_MOUSE_LL = 14;

    HookProc MouseHookProcedure;


    [StructLayout(LayoutKind.Sequential)]
    public class POINT
    {
        public int x;
        public int y;
    }


    [StructLayout(LayoutKind.Sequential)]
    public class MouseHookStruct
    {
        public POINT pt;
        public int hWnd;
        public int wHitTestCode;
        public int dwExtraInfo;
    }

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetModuleHandle(string name);


    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);


    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern bool UnhookWindowsHookEx(int idHook);


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


    public MouseHook()
    {
    }


    ~MouseHook()
    {
        Stop();
    }

    public void Start()
    {

        if (hMouseHook == 0)
        {
            MouseHookProcedure = new HookProc(MouseHookProc);

            //hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]), 0);
            hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);


            if (hMouseHook == 0)
            {
                Stop();
                throw new Exception("SetWindowsHookEx failed.");
            }
        }
    }

    public void Stop()
    {
        bool retMouse = true;
        if (hMouseHook != 0)
        {
            retMouse = UnhookWindowsHookEx(hMouseHook);
            hMouseHook = 0;
        }


        if (!(retMouse)) throw new Exception("UnhookWindowsHookEx failed.");
    }

    private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
    {

        if ((nCode >= 0) && (OnMouseActivity != null))
        {
            MouseButtons button = MouseButtons.None;
            int clickCount = 0;

            switch (wParam)
            {
                case WM_LBUTTONDOWN:
                    button = MouseButtons.Left;
                    clickCount = 1;
                    break;
                case WM_LBUTTONUP:
                    button = MouseButtons.Left;
                    clickCount = 2;
                    break;
                case WM_LBUTTONDBLCLK:
                    button = MouseButtons.Left;
                    clickCount = 3;
                    break;
                case WM_RBUTTONDOWN:
                    button = MouseButtons.Right;
                    clickCount = 4;
                    break;
                case WM_RBUTTONUP:
                    button = MouseButtons.Right;
                    clickCount = 5;
                    break;
                case WM_RBUTTONDBLCLK:
                    button = MouseButtons.Right;
                    clickCount = 6;
                    break;
            }


            MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
            System.Windows.Forms.MouseEventArgs e = new System.Windows.Forms.MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0);

            OnMouseActivity(this, e);
        }
        return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
    }
}
0 Answers
Related