本文介绍了C ++中的全局钩子需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友

我正在尝试设置全局挂钩WH_CBT.

用HookProcedure创建了一个Dll

LRESULT CALLBACK WindowHookProc(int code, WPARAM wParam, LPARAM lParam)


已经创建了一个基于桌面的应用程序,我正在其中安装挂钩

m_hWindowHook= SetWindowsHookEx(WH_CBT,hkprcSysMsg,m_hHookDll,0);

现在,Hook已正确安装,但未全局调用HookProcedure.

仅针对桌面应用程序&不适用于其他应用

任何想法,我在做什么错?

Hey Friends

I am trying to setup a global hook WH_CBT.

Have Created a Dll with HookProcedure

LRESULT CALLBACK WindowHookProc(int code, WPARAM wParam, LPARAM lParam)


Have Created a Desktop based Application in which i am installing the hook

m_hWindowHook= SetWindowsHookEx(WH_CBT,hkprcSysMsg,m_hHookDll,0);

Now Hook gets installed properly, but the HookProcedure is not called globally.

It is being called only for the desktop application & not for other applications

Any idea, what wrong i am doing ?

推荐答案

// <span class="code-keyword">this: hook.cpp</span>
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

#pragma comment(lib,"User32.lib")

#define  TERM_MESSAGE  __TEXT("{0C3ED513-F38C-4996-8130-F9A3C93D890B}")
HANDLE                __hTERM = 0;
HHOOK                  __ahooks[WH_MAX] = {0};

void InstallHooks(HMODULE h);
void RemoveHooks();
void __loggi(const TCHAR* t,...);

int  Continue()
{
  return WAIT_TIMEOUT==WaitForSingleObject(__hTERM,0);
}

void LogHookData(int c,WPARAM w,LPARAM l,const TCHAR* idHook)
{
  TCHAR    mod[0x400];
  GetModuleFileName(0,mod,sizeof(mod)/sizeof(mod[0]));
  __loggi(__TEXT("%s: %s\r\n"),idHook,mod);
  __loggi(__TEXT("  c: %i; w: %08X; l: %08X\r\n"),c,(unsigned int)w,(unsigned int)l);
}


LRESULT FAR PASCAL __fnWH_SHELL(int c,WPARAM w,LPARAM l)
{
  if(0>c) return CallNextHookEx(__ahooks[WH_SHELL],c,w,l);
  if(!Continue()){ RemoveHooks(); return 0; }
  LogHookData(c,w,l,__TEXT("WH_SHELL"));
  return 0;
}

LRESULT FAR PASCAL __fnWH_CBT(int c,WPARAM w,LPARAM l)
{
  if(0>c) return CallNextHookEx(__ahooks[WH_CBT],c,w,l);
  if(!Continue()){ RemoveHooks(); return 0; }
  LogHookData(c,w,l,__TEXT("WH_CBT"));
  return 0;
}

LRESULT FAR PASCAL __fnWH_GETMESSAGE(int c,WPARAM w,LPARAM l)
{
  if(0>c) return CallNextHookEx(__ahooks[WH_GETMESSAGE],c,w,l);
  if(!Continue()){ RemoveHooks(); return 0; }
  LogHookData(c,w,l,__TEXT("WH_GETMESSAGE"));
  return 0;
}

void InstallHooks(HMODULE h)
{
  unsigned int  wh;

  __hTERM = OpenEvent(EVENT_ALL_ACCESS,0,TERM_MESSAGE);
  if(Continue())
    for(wh=0;wh<(sizeof(__ahooks)/sizeof(__ahooks[0]));wh++)
    {
      if(__ahooks[wh]) continue;
      switch(wh)
      {
        case WH_CBT:
          __ahooks[wh] = SetWindowsHookEx(wh,__fnWH_CBT,h,0);
        break;
        case WH_SHELL:
          // __ahooks[wh] = SetWindowsHookEx(wh,__fnWH_SHELL,h,0);
        break;
        case WH_GETMESSAGE:
          // __ahooks[wh] = SetWindowsHookEx(wh,__fnWH_GETMESSAGE,h,0);
        break;
      }
    }
}

void RemoveHooks()
{
  unsigned int  wh;
  for(wh=0;wh<(sizeof(__ahooks)/sizeof(__ahooks[0]));wh++)
  {
    if(0==__ahooks[wh]) continue;
    UnhookWindowsHookEx(__ahooks[wh]);
    __ahooks[wh] = 0;
  }
  if(__hTERM) CloseHandle(__hTERM); __hTERM = 0;
}

int FAR PASCAL DllMain(HMODULE h,DWORD r,void* p)
{
  switch(r)
  {
    case DLL_PROCESS_ATTACH: InstallHooks(h); break;
    case DLL_PROCESS_DETACH: RemoveHooks(); break;
    case DLL_THREAD_ATTACH : break;
    case DLL_THREAD_DETACH : break;
  }
  return 1;
}

///////////////////////////////////////////
// logging
class llock
{
public:
  llock(const TCHAR* f)
  {
    TCHAR          fm[MAX_PATH];
    unsigned int  i;
    _tcscpy_s(fm,sizeof(fm)/sizeof(fm[0]),_f=f);
    for(i=0;fm[i];i++) fm[i]='\\'==fm[i]?'/':fm[i];
    _h = CreateMutex(0,0,fm);
  }
  llock()
  {
    CloseHandle(_h);
  }
  
  operator HANDLE (){ return _h; }
  operator const TCHAR* (){ return _f; }

private:
  HANDLE        _h;
  const TCHAR*  _f;

} __loggi_lock(__TEXT("c:\\temp\\hooks.txt"));

void __loggi(const TCHAR* t,...)
{
  HANDLE        hf;
  
  WaitForSingleObject(__loggi_lock,INFINITE);
  hf = ::CreateFile(__loggi_lock,GENERIC_WRITE,0,0,OPEN_ALWAYS,0,0);
  if(INVALID_HANDLE_VALUE!=hf)
  {
    unsigned long    w = 0;
    va_list          val;
    TCHAR            f[0x1000];
    int              l;

    va_start(val,t);
    l = _vsntprintf_s(f,sizeof(f)/sizeof(f[0]),_TRUNCATE,t,val);
    va_end(val);
    if(0==SetFilePointer(hf,0,0,FILE_END))
    {
      if(sizeof(short)==sizeof(TCHAR))
      {
        unsigned short  unicode = 0xFeFF;
        WriteFile(hf,(void*)&unicode,2,&w,0);
      }
    }
    WriteFile(hf,(void*)f,l*sizeof(TCHAR),&w,0);
    CloseHandle(hf);
  }
  ReleaseMutex(__loggi_lock);
}


main.cpp的内容:


content of main.cpp:

// <span class="code-keyword">this: main.cpp</span>
#pragma once
#include <windows.h>
#include <tchar.h>
#pragma comment(lib,"User32.lib")


#ifdef _WIN64
  #define  ON64(C,E)  C
#else
  #define  ON64(C,E)  E
#endif

#ifdef _DEBUG
  #include <crtdbg.h>
  #define  START()  { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); }
#else
  #define  START()  
#endif

int _tmain(int argc, _TCHAR* argv[])
{
  /****/ START() /****/

  HANDLE    hterm = CreateEvent(0,1,0,__TEXT("{0C3ED513-F38C-4996-8130-F9A3C93D890B}"));
  HINSTANCE  hdll = LoadLibrary(ON64(__T("hooks64.dll"),__T("hooks32.dll")));
  if((HINSTANCE)32<hdll)
  {
    _tprintf(__T("loaded... <key>")); _gettch();
    SetEvent(hterm);
    FreeLibrary(hdll);
  }
  CloseHandle(hterm);
  return 0;
}


此示例为CBT设置了全局挂钩,并记录了所有模块中的所有事件.
您必须为64位应用程序构建一个应用程序,为32位应用程序构建一个应用程序.
祝你好运.


This example sets a global hook for CBT and logs all events in all modules.
You have to build one for 64-bit and one for 32-bit applications.
Good luck.




这篇关于C ++中的全局钩子需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 13:15