C# 钩子 hwnd 始终为 0

发布于 2024-12-14 08:15:12 字数 3608 浏览 1 评论 0原文

我有一个附加到以下回调的钩子。它运行良好,但是,MOUSEHOOKSTRUCT 的 hwnd 始终为零。

private static IntPtr SetHook(LowLevelMouseProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            IntPtr ptr = GetModuleHandle(curModule.ModuleName);
            return SetWindowsHookEx(WH_MOUSE_LL, proc,
                GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        { 
            MOUSEHOOKSTRUCT msg = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));

            PictureBox control = Control.FromHandle(msg.hwnd) as PictureBox;

            if (control != null)
            {
                PreviewForm.pbox_MouseClick(control, new MouseEventArgs(MouseButtons.Left, 2, msg.pt.x, msg.pt.y, 0));
            }                
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    private const int WH_MOUSE_LL = 14;

    private enum MouseMessages
    {
        WM_LBUTTONDOWN = 0x0201,
        WM_LBUTTONUP = 0x0202,
        WM_MOUSEMOVE = 0x0200,
        WM_MOUSEWHEEL = 0x020A,
        WM_RBUTTONDOWN = 0x0204,
        WM_RBUTTONUP = 0x0205
    }

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

     [StructLayout(LayoutKind.Sequential)]
    struct MOUSEHOOKSTRUCT
    {
        public POINT pt;
        public IntPtr hwnd;
        public uint wHitTestCode;
        public uint dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct MSLLHOOKSTRUCT
    {
        public POINT pt;
        public uint mouseData;
        public uint flags;
        public uint time;
        public IntPtr dwExtraInfo;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook,
        LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

更新

至少部分问题是我应该将 lParam 转换为 MSLLHOOKSTRUCT 而不是 MOUSEHOOKSTRUCT。它没有 hwnd,所以我想我只是被搞砸了。

http://msdn.microsoft。 com/en-us/library/windows/desktop/ms644970(v=vs.85).aspx

更新

已解决:我将其更改为:

private static IntPtr SetHook(LowLevelMouseProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            IntPtr ptr = GetModuleHandle(curModule.ModuleName);
            return SetWindowsHookEx(WH_MOUSE, proc,
                IntPtr.Zero, (uint)AppDomain.GetCurrentThreadId());
        }
    } 

问题是我使用的是 LL 钩子而不是普通钩子。因此,我选择了错误的结构。

I have a hook attached to the following callback. It fires fine, however, the hwnd is always zero for the MOUSEHOOKSTRUCT.

private static IntPtr SetHook(LowLevelMouseProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            IntPtr ptr = GetModuleHandle(curModule.ModuleName);
            return SetWindowsHookEx(WH_MOUSE_LL, proc,
                GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        { 
            MOUSEHOOKSTRUCT msg = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));

            PictureBox control = Control.FromHandle(msg.hwnd) as PictureBox;

            if (control != null)
            {
                PreviewForm.pbox_MouseClick(control, new MouseEventArgs(MouseButtons.Left, 2, msg.pt.x, msg.pt.y, 0));
            }                
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    private const int WH_MOUSE_LL = 14;

    private enum MouseMessages
    {
        WM_LBUTTONDOWN = 0x0201,
        WM_LBUTTONUP = 0x0202,
        WM_MOUSEMOVE = 0x0200,
        WM_MOUSEWHEEL = 0x020A,
        WM_RBUTTONDOWN = 0x0204,
        WM_RBUTTONUP = 0x0205
    }

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

     [StructLayout(LayoutKind.Sequential)]
    struct MOUSEHOOKSTRUCT
    {
        public POINT pt;
        public IntPtr hwnd;
        public uint wHitTestCode;
        public uint dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct MSLLHOOKSTRUCT
    {
        public POINT pt;
        public uint mouseData;
        public uint flags;
        public uint time;
        public IntPtr dwExtraInfo;
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook,
        LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

update

At least part of the problem is that I should be casting lParam to MSLLHOOKSTRUCT Not MOUSEHOOKSTRUCT. It doesn't have an hwnd, so I guess I am just screwed.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644970(v=vs.85).aspx

update

Solved: I changed this to:

private static IntPtr SetHook(LowLevelMouseProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            IntPtr ptr = GetModuleHandle(curModule.ModuleName);
            return SetWindowsHookEx(WH_MOUSE, proc,
                IntPtr.Zero, (uint)AppDomain.GetCurrentThreadId());
        }
    } 

The problem was that I was using the LL hook and not the normal hook. Hence, I was casting to the wrong structure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怂人 2024-12-21 08:15:12

这可能没问题,具体取决于您安装挂钩的方式。根据 WinUser.h:

#define HWND_DESKTOP        ((HWND)0)    

因此窗口句柄为零可能表明您正在拦截桌面窗口的消息。

This may be fine depending on how you have installed the hook. According to WinUser.h:

#define HWND_DESKTOP        ((HWND)0)    

So a window handle of zero might indicate that you are intercepting message for the Desktop Window.

无人问我粥可暖 2024-12-21 08:15:12

我使用的是 WH_MOUSE_LL 而不是 WH_MOUSE,因此该行应该是

MOUSEHOOKSTRUCT msg = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));

,但 WH_MOUSE_LL 使用 MSLLHOOKSTRUCT。我必须更改挂钩才能使用 WH_MOUSE。最终代码为:

private static IntPtr SetHook(LowLevelMouseProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            IntPtr ptr = GetModuleHandle(curModule.ModuleName);
            return SetWindowsHookEx(WH_MOUSE, proc,
                IntPtr.Zero, (uint)AppDomain.GetCurrentThreadId());
        }
    }

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        { 
            MOUSEHOOKSTRUCT msg = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));

            PictureBox control = Control.FromHandle(msg.hwnd) as PictureBox;

            if (control != null)
            {
                PreviewForm.pbox_MouseClick(control, new MouseEventArgs(MouseButtons.Left, 2, msg.pt.x, msg.pt.y, 0));
            }                
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

I was using WH_MOUSE_LL instead of WH_MOUSE, thus the line should have been

MOUSEHOOKSTRUCT msg = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));

but WH_MOUSE_LL uses MSLLHOOKSTRUCT. I had to change the hook to use WH_MOUSE. The final code is:

private static IntPtr SetHook(LowLevelMouseProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            IntPtr ptr = GetModuleHandle(curModule.ModuleName);
            return SetWindowsHookEx(WH_MOUSE, proc,
                IntPtr.Zero, (uint)AppDomain.GetCurrentThreadId());
        }
    }

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        { 
            MOUSEHOOKSTRUCT msg = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));

            PictureBox control = Control.FromHandle(msg.hwnd) as PictureBox;

            if (control != null)
            {
                PreviewForm.pbox_MouseClick(control, new MouseEventArgs(MouseButtons.Left, 2, msg.pt.x, msg.pt.y, 0));
            }                
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文