CallWndProc 示例

nwn*_*oga 1 .net c# hook

这是我第一次尝试hooks

我正在寻找一些好的资源来实施CallWndProc hook. MSDN 的东西有点让人不知所措。

我发现使用这种类型的钩子需要注入外部 dll。这主要是我被困住的地方。

不确定 dll 中需要包含什么以及.NET应用程序中需要包含什么。

有dll例子吗?

Cod*_*ray 5

您无法WH_CALLWNDPROC使用 C# 等托管语言编写挂钩。因此,您需要的不仅仅是一个外部 DLL,还需要一个用可编译为本机代码的语言(如 C 或 C++)编写的外部 DLL。

MSDN 文档其实相当不错,尤其是概述使用 Hooks页面上甚至还有一个示例。

我的意思并不是要让你感到沮丧,但如果你发现这令人难以承受,那么你将很难让它正常工作。挂钩是 Windows 编程中非常先进的技术。在进行这样的项目之前,您需要了解窗口过程、消息循环和 Windows 应用程序的其他基础知识。熟悉 C 或 C++ 语言显然也有帮助,因为这就是您将要使用的语言!

不管怎样,我正好手边有一个用 C 语言编写的钩子 DLL,所以我会尝试提取一些相关代码。它实际上安装了一个WH_CALLWNDRETPROC钩子,但两者非常相似。这个挂钩过程在窗口过程处理完消息后被调用;您正在谈论的那个是在窗口过程处理消息之前调用的。

/* The handle to the hook is stored as a shared global variable and is the
 * same for all hooked processes. We achieve that by placing it in the
 * shared data segment of the DLL.
 *
 * Note that shared global variables must be explicitly initialized.
 *
 * And also note that this is really not the ideal way of doing this; it's just
 * an easy way to get going. The better solution is to use a memory-mapped file.
 * See Also: http://msdn.microsoft.com/en-us/library/h90dkhs0.aspx
 */
#pragma comment(linker, "/section:.SHARED,rws")
#pragma data_seg(".SHARED") /* begin the shared data segment */
   HHOOK g_hhkCallWndProcRet = NULL;
#pragma data_seg()          /* end the shared data segment and default back to normal behavior */


LRESULT CALLBACK CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
{
   /* If nCode is greater than or equal to HC_ACTION,
    * we should process the message. */
   if (nCode >= HC_ACTION)
   {
      /* Retrieve a pointer to the structure that contains details about
       * the message, and see if it is one that we want to handle. */
      const LPCWPRETSTRUCT lpcwprs = (LPCWPRETSTRUCT)lParam;
      switch (lpcwprs->message)
      {
         /* ...SNIP: process the messages we're interested in ... */
      }
   }

   /* At this point, we are either not processing the message
    * (because nCode is less than HC_ACTION),
    * or we've already finished processing it.
    * Either way, pass the message on. */
   return CallNextHookEx(g_hhkCallWndProcRet, nCode, wParam, lParam);
}


BOOL __stdcall InstallHook(void)
{
   /* Try to install the WH_CALLWNDPROCRET hook,
    * if it is not already installed. */
   if (!g_hhkCallWndProcRet)
   {
      g_hhkCallWndProcRet = SetWindowsHookEx(WH_CALLWNDPROCRET,
                                             CallWndRetProc,
                                             g_hinstDLL,
                                             0);
      if (!g_hhkCallWndProcRet)
      {
         /* ...SNIP: handle failure condition ... */
         return FALSE;
      }
   }

   return TRUE;  /* return success */
}

BOOL __stdcall RemoveHook(void)
{
   /* Try to remove the WH_CALLWNDPROCRET hook, if it is installed. */
   if (g_hhkCallWndProcRet)
   {
      if (!UnhookWindowsHookEx(g_hhkCallWndProcRet))
      {
         /* ...SNIP: handle failure condition ... */
         return FALSE;
      }
      g_hhkCallWndProcRet = NULL;
   }

   return TRUE;  /* return success */
}
Run Code Online (Sandbox Code Playgroud)