标签:hook
Hook(钩子)就是对Windows系统的一些消息或是API函数进行拦截和监控的处理平台,让可以根据程序员的设置获取其感兴趣的信息。
这里主要是介绍一下Hook拦截鼠标消息和键盘消息。
下面是CALLBACK Proc 回调函数 和 CallNextHookEx函数
LRESULT CALLBACK HookProc
(
  int nCode, //指定是否需要处理该消息 
WPARAM wParam, 
LPARAM lParam //包含该消息的附加消息 ,
);
这个回调函数的名字可以随你取,但形式可一定要满足以上要求,其实钩子的回调函数和Windows的差不多一个德行。看看钩子函数的返回值,若是返回非0值,表示我们已经自己处理了该消息,则消息就不被传递到目标窗口过程。
看看LRESULT CallNextHookEx
(HHOOK hhk;
int nCode;
WPARAM wParam;
LPARAM lParam;)这个函数把钩子信息传递给下一个钩子函数,也就是可以理解成把车辆放行到下一个检查站,这个可以根据自己的需要进行调用。若是我们只设定了一个钩子函数,那么我们假设把钩子消息用CallNextHookEx传给下个钩子函数,因为不存在所以就传递回了目标窗口函数。
先创建一个MFC BaseDialog 工程
XXXDlg.cpp代码
// MbHookDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MbHook.h"
#include "MbHookDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();
// Dialog Data
	enum { IDD = IDD_ABOUTBOX };
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CMbHookDlg dialog
CMbHookDlg::CMbHookDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMbHookDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMbHookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMbHookDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
<span style="color:#3333ff;"><//////////////////////////////////////////////////////////
//Hook的过程函数
HHOOK hook;
HWND hWnd;
LRESULT CALLBACK MessageBoxProc(
	INT nCode,WPARAM wParam,LPARAM lParam
	)
{
	if(nCode<0)
	return CallNextHookEx(hook,nCode,wParam,lParam);
	if (wParam == VK_F3)//当按“F3”键时卸载Hook
	{
		EndDialog(hWnd,NULL);//关闭窗口
		UnhookWindowsHookEx(hook);//卸载HOOk函数
	}else
	{
		return 1;
	}
	
}</span>
// CMbHookDlg message handlers
BOOL CMbHookDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	// Add "About..." menu item to system menu.
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	// TODO: Add extra initialization here
<span style="color:#3333ff;">	hWnd= m_hWnd;
	hook=SetWindowsHookEx(//WH_MOUSE//设置鼠标钩子
		WH_KEYBOARD//设置键盘钩子
		,MessageBoxProc,NULL,GetCurrentThreadId());</span>
	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CMbHookDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}
// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.
void CMbHookDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}
// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMbHookDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
HHOOK SetWindowsHookEx(
标签:hook
原文地址:http://blog.csdn.net/u013147600/article/details/45536127