WinApi - 覆盖WH_GETMESSAGE钩子中的GetMessage循环

时间:2016-01-10 11:07:43

标签: c# .net winapi user32

我已经使用以下消息循环实现了标准的Win32 MDI窗口:

 MSG msg;
 while (NativeMethods.GetMessage(out msg, this.handle, 0, 0) > 0)
 {
       if (!NativeMethods.TranslateMDISysAccel(this.mdiClientHandle, ref msg))
            {
                NativeMethods.TranslateMessage(ref msg);
                NativeMethods.DispatchMessage(ref msg);
            }
        }
  }

我试图通过使用WH_GETMESSAGE挂钩来覆盖此消息循环。钩子设置正确并且有效。

  private static IntPtr MyGetMessageHook(int code, IntPtr wparam, IntPtr lparam)
    {
        if (code == 0)
        {
            MSG msg = (MSG)Marshal.PtrToStructure(lparam, typeof(MSG));

            Debug.WriteLine(msg.message);

            // Execute GetMessage loop logic.
            if (!NativeMethods.TranslateMDISysAccel(MyMdiWindow.MdiClientHandle, ref msg))
            {
                NativeMethods.TranslateMessage(ref msg);
                NativeMethods.DispatchMessage(ref msg);
            }

            // Set msg fields to 0.
            msg.message = 0;
            msg.hwnd = IntPtr.Zero;
            msg.lParam = IntPtr.Zero;
            msg.wParam = IntPtr.Zero;

            Marshal.StructureToPtr(msg, lparam, true);
        }

        return new IntPtr(NativeMethods.CallNextHookEx(IntPtr.Zero, code, wparam, lparam));
    }

挂钩程序中的代码执行,但我不知道为什么窗口没有响应。调度后,msg.message被设置为0。调用DispatchMessage后,消息会发生什么?

0 个答案:

没有答案