c#钩子hwnd总是0

本文关键字:总是 hwnd 钩子 | 更新日期: 2023-09-27 18:13:45

我有一个钩子附加到下面的回调。它可以正常运行,但是,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。它没有手柄,所以我想我完蛋了。

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钩子而不是普通钩子。因此,我将铸件铸造到错误的结构。

c#钩子hwnd总是0

这可能很好,这取决于您如何安装钩子。根据WinUser.h:

#define HWND_DESKTOP        ((HWND)0)    

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

我使用的是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);
    }