C# hook hwnd is always 0

烈酒焚心 提交于 2020-01-15 12:42:24

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/8054287/c-sharp-hook-hwnd-is-always-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!