CallWndProc Example

给你一囗甜甜゛ 提交于 2021-01-05 11:20:44

问题


This is my first time experimenting with hooks.

I'm looking for some good resources for implementing a CallWndProc hook. The MSDN stuff is a bit overwhelming.

I have discovered that with this type of hook a external dll needs to be injected. That's mainly where I'm stuck.

Not sure what needs to be in the dll and what needs to be in the .NET app.

Any dll examples?


回答1:


You cannot write a WH_CALLWNDPROC hook in a managed language like C#. So you need more than just an external DLL, you need an external DLL written in a language that compiles down to native code, like C or C++.

The MSDN documentation is actually pretty good, especially the overview. There's even an example on the Using Hooks page.

I don't mean to sound discouraging, but if you find that overwhelming, you're going to have a fair bit of trouble getting this to work right. Hooks are a very advanced technique in Windows programming. You need to understand window procedures, message loops, and the other basics of Windows applications before you undertake a project like this one. It also obviously helps to know either the C or C++ languages well, since that's what you'll be using!

Anyway, I just happen to have a hook DLL that I've written in C handy, so I'll try to pull out some of the relevant code. It actually installs a WH_CALLWNDRETPROC hook, but the two are quite similar. The hook procedure for this one is called after the window procedure has processed the message; the one you're talking about is called before the window procedure has processed the message.

/* 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 */
}


来源:https://stackoverflow.com/questions/17747345/callwndproc-example

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