vs2010新建一个空的win32项目取名dllhook
新建dllhook.def键入如下代码
LIBRARY dllhook
EXPORTS
SetKeyBoardHook @123
新建dllhook.h键入如下代码
#ifndef DLLHOOK_HEAD_FILE
#define DLLHOOK_HEAD_FILE
#include
//导出定义
#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
#include
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
#include
#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;
}