一个简单的全局HOOK(C++)

1>HOOK头文件(Hook.h)

#pragma once
//#define WM_SENDFILES WM_USER + 1
#ifdef Hook_Dll_API 
#else
#define Hook_Dll_API _declspec(dllimport)
#endif
#include
Hook_Dll_API void SetHook(HWND g_hWnd);

2>源文件(Hook.cpp)

#include "Hook.h"


HHOOK g_hMouse = NULL ;
HHOOK g_hKeyboard = NULL ;
HWND g_hWnd = NULL ;

LRESULT CALLBACK MouseProc(          //鼠标钩子的过程函数
          int nCode,
          WPARAM wParam,
          LPARAM lParam)
{
return 1 ;              //return 1;可以截获所有的鼠标消息
}                  //return 0 ;表示未对鼠标消息作出任何动作,把该消息教给系统

LRESULT CALLBACK KeyBoardProc(   //键盘钩子的过程函数

          int nCode,
          WPARAM wParam,
          LPARAM lParam)
{

if(VK_RETURN == wParam)
{
   ::SendMessage(g_hWnd,WM_CLOSE,0,0);
   UnhookWindowsHookEx(g_hKeyboard);
   UnhookWindowsHookEx(g_hMouse);
}
return 1 ;                  //同鼠标
}

void SetHook(HWND g_hWnd)        //设置钩子
{
g_hMouse = SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle(L"Hook"),0);
g_hKeyboard = SetWindowsHookEx(WH_KEYBOARD,KeyBoardProc,GetModuleHandle(L"Hook"),0);
}

3>Hook.def文件

LIBRARY "HOOK"

EXPORTS

SetHook

 

 

原文:http://hi.baidu.com/lightningyaoyao/blog/item/7acda91107c966c4a7ef3f5d.html

你可能感兴趣的:(C/C++)