钩子遇到的第一个问题

写了第一个钩子,全局的屏记事本的输入,结果无效。

代码如下:

#include "stdio.h"
#include "windows.h"

#define DEF_PROCESS_NAME  "notepad.exe"

HINSTANCE g_hInstance = NULL;
HHOOK g_hHook = NULL;
HWND g_hWnd = NULL;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
{
 switch (dwReason)
 {
 case DLL_PROCESS_ATTACH:
  g_hInstance = hinstDLL;
  break;

 case DLL_PROCESS_DETACH:
  break;
 }

 return TRUE;
}

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 char szPath[MAX_PATH] = { 0, };
 char *p = NULL;

 if (nCode >= 0)
 {
  // bit 31 : 0 => press, 1 => release
  if (!(lParam & 0x80000000))
  {
   GetModuleFileNameA(0, szPath, MAX_PATH);
   p = strrchr(szPath, '\\');

    if (!_stricmp(p + 1, DEF_PROCESS_NAME))
    return 1;
  }
 }

 return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

#ifdef __cplusplus
extern "C" {
#endif
 __declspec(dllexport) void HookStart()
 {
  g_hHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstance, 0);
 }

 __declspec(dllexport) void HookStop()
 {
  if (g_hHook)
  {
   UnhookWindowsHookEx(g_hHook);
   g_hHook = NULL;
  }
 }
#ifdef __cplusplus
}
#endif

 

后来想明白了,32位的DLL对64位的记事本无效。。。

局部钩子与全局钩子的区别。。。绑定线程ID

posted @ 2015-05-12 01:27  笔直的一道弯  阅读(215)  评论(0编辑  收藏  举报