标签:com style code c t sp ext strong get color int
一、首先创建一个dll工程,取名为KeyboardHookDll,代码如下:
// KeyboardHookDll.cpp : 定义 DLL 应用程序的导出函数。
//
#include
"stdafx.h"
#include <iostream>
using namespace std;
#define
MYAPI extern "C" _declspec(dllexport) //导出函数声明
HHOOK hHook =
NULL;
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM
lParam)
{
if (code >= 0)
return
1;
//消息不再传递个下一个HOOK子程,也不会再发送给目的窗口
else
return CallNextHookEx(hHook,
HC_ACTION, wParam, lParam);
}
MYAPI int
SetHook()
{
hHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc,
GetModuleHandleA("KeyboardHookDll.dll"), 0);
if (hHook ==
NULL)
{
printf("SetWindowsHookEx() error :%d\n",
GetLastError());
return -1;
}
return
0;
}
MYAPI int StopHook()
{
if
(UnhookWindowsHookEx(hHook) ==
FALSE)
{
printf("UnhookWindowsHookEx() error
:%d\n");
return -1;
}
return 0;
}
二、新建一个控制台程序,用来启用HOOK,代码如下:
#include "stdafx.h"
#include "windows.h"
#include
<iostream>
#pragma comment(lib,"KeyboardHookDll.lib")
using
namespace std;
extern "C" _declspec(dllimport) void
SetHook();
extern "C" _declspec(dllimport) void StopHook();
int
_tmain(int argc, _TCHAR* argv[])
{
char
YesNo;
printf("这是一个关于全局键盘钩子的测试...\n");
printf("安装全局键盘钩子...\n");
SetHook();
printf("是否卸载键盘钩子:
(Y or N)\n"); //当然了,下面的语句根本无法执行
YesNo =
getchar();
if (YesNo == ‘Y‘ || YesNo ==
‘y‘)
{
printf("开始卸载钩子...\n\n");
StopHook();
printf("钩子已经卸载...\n");
}
system("pause");
return 0;
}
标签:com style code c t sp ext strong get color int
原文地址:http://www.cnblogs.com/duyy/p/3701067.html