本文介绍了CallWndProc示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试hooks

我正在寻找一些好的资源来实现CallWndProc hook。MSDN的东西有点压倒性。

我发现,使用这种类型的挂钩需要注入外部DLL。这主要是我陷入困境的原因。

不确定DLL中需要包含哪些内容以及.NET应用程序中需要包含哪些内容。

有没有DLL示例?

推荐答案

您不能用C#这样的托管语言编写WH_CALLWNDPROC钩子。因此,您需要的不仅仅是一个外部DLL,您还需要一个用向下编译为本机代码的语言编写的外部DLL,如C或C++。

The MSDN documentation其实还不错,尤其是overviewUsing 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 */
}

这篇关于CallWndProc示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:51