SetWindowsHookEx设置全局键盘钩子

vs2010新建一个空的win32项目取名dllhook

 

新建dllhook.def键入如下代码

LIBRARY dllhook

EXPORTS  
SetKeyBoardHook  @123


 

新建dllhook.h键入如下代码

#ifndef DLLHOOK_HEAD_FILE
#define DLLHOOK_HEAD_FILE
#include <Windows.h>

//导出定义
#ifndef DLLHOOK_API
#ifdef  DLLHOOK_DLL
#define DLLHOOK_API _declspec(dllexport)
#else
#define DLLHOOK_API _declspec(dllimport)
#endif
#endif

//模块定义
#ifndef _DEBUG
#define DLLHOOK_DLL_NAME	TEXT("dll2.dll")			//组件名字
#else
#define DLLHOOK_DLL_NAME	TEXT("dll2D.dll")			//组件名字
#endif


bool SetKeyBoardHook(HWND hwnd);
//
//导出文件

#ifndef DLLHOOK_DLL
#include "dllhook.h"
#endif

//

#endif


 

新建dllhook.cpp键入如下代码

#include "dllhook.h"
#include <assert.h>
#include <windows.h>

HHOOK g_hook=NULL;

#pragma data_seg("my_data")
HWND g_hwnd=NULL;
#pragma data_seg()
#pragma comment (linker,"/section:my_data,RWS")

BOOL APIENTRY DllMain(HMODULE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		{
			OutputDebugString(TEXT("加载dll"));
		}
		break;
	case DLL_THREAD_ATTACH:
		{
			OutputDebugString(TEXT("新建线程"));
		}
		break;
	case DLL_THREAD_DETACH:
		{
			OutputDebugString(TEXT("线程退出"));
		}
		break;
	case DLL_PROCESS_DETACH:
		{
			OutputDebugString(TEXT("释放dll"));
			if (g_hook)
				UnhookWindowsHookEx(g_hook);
		}
		break;
	}
	return TRUE;
}

LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
	if (VK_F2==wParam && (lParam>>31 &1)==0)
	{
		MessageBox(NULL,TEXT("测试"),NULL,MB_OK);
		return true;
	}
	else if (VK_F4==wParam && (lParam>>31 &1)==0)
	{
		SendMessage(g_hwnd,WM_CLOSE,0,0);
		if (g_hook)
			UnhookWindowsHookEx(g_hook);
		return true;
	}
	else
		return CallNextHookEx(g_hook,code,wParam,lParam);

	assert(false);
	return false;
}

bool SetKeyBoardHook(HWND hwnd)
{
	g_hwnd=hwnd;
	g_hook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle(TEXT("dllhook.dll")),NULL);
	if (g_hook==NULL)
	{
		assert(false);
		MessageBox(NULL,TEXT("设置HOOK失败"),NULL,MB_OK);
		return false;
	}
	return true;
}


 

exe部分文件一个空的win32项目取名1,设置在静态库中使用MFC

1.h键入如下代码

#ifndef MFCTEST_HEAD
#define MFCTEST_HEAD
#pragma once

#include <afxwin.h>
#include <afxframewndex.h>

#define IDC_START   1012            //按钮定义

class CMyApp:public CWinApp
{
public:
	//构造函数
	CMyApp();
	//析构函数
	virtual ~CMyApp();

	//重载函数
public:
	//初始函数
	virtual BOOL InitInstance();
};



class CMyWindow : public CFrameWndEx
{
public:
	//构造函数
	CMyWindow();
	//析构函数
	virtual ~CMyWindow();

	//虚函数
public:
	//命令响应消息
	virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);


	//消息映射
public:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	DECLARE_MESSAGE_MAP()

public:
	CButton       m_button;       //按钮

};



#endif


 

1.cpp键入如下代码

#include "1.h"
#include "..\\..\\dllhook\dllhook\dllhook.h"
#pragma comment (lib,"dllhook.lib")

CMyApp myapp;

//构造函数
CMyApp::CMyApp()
{

}

//析构函数
CMyApp::~CMyApp()
{

}

//初始函数
BOOL CMyApp::InitInstance()
{
	m_pMainWnd = new CMyWindow();
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}



//{{AFX_MSG_MAP(CFrameWndEx)
BEGIN_MESSAGE_MAP(CMyWindow, CFrameWndEx)
	ON_WM_CREATE()
END_MESSAGE_MAP()


//构造函数
CMyWindow::CMyWindow()
{
	Create(NULL, TEXT("My Window"));
	RECT rect={0,0,50,30};
	m_button.Create(TEXT("按钮"),WS_CHILD|WS_VISIBLE,rect,this,IDC_START);
}
//析构函数
CMyWindow::~CMyWindow()
{
}

//命令响应消息
BOOL CMyWindow::OnCommand( WPARAM wParam, LPARAM lParam )
{
	int wmId=LOWORD(wParam);
	switch (wmId)
	{
	case IDC_START:
		{
			MessageBox(TEXT("测试一下哈"),TEXT("提示"),MB_OK);
			return true;
		}
		break;
	default:
		return true;
	}

	return __super::OnCommand(wParam,lParam);

}

//创建事件
int CMyWindow::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
	__super::OnCreate(lpCreateStruct);
	SetKeyBoardHook(m_hWnd);
	return true;
}


 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值