<b>用键盘全局钩子Hook监督多进程键盘操作</b>[VC/C++编程]
本文“<b>用键盘全局钩子Hook监督多进程键盘操作</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
闲来无事,在WIN2K下用BCB5做了个键盘挂钩小程序,监督全局按键情形.Hook安设和回调函数放在一个单独DLL中,DLL原码以下:
//----------------------------------------------------------------------------------------------------
extern "C" __declspec(dllexport) void __stdcall SetHook(HWND,bool);
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
//----------------------------------------------------------------------------------------------------
static HINSTANCE hInstance; // 利用实例句柄
static HWND hWndMain; // MainForm句柄
static HHOOK hKeyHook; // HOOK句柄
static const myMessage=2000; // 自定义消息号
static const SecondPar=1; // 自定义消息第2参数
//----------------------------------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{ hInstance=hinst; return 1; }
//----------------------------------------------------------------------------------------------------
void __stdcall SetHook(HWND hMainWin,bool nCode)
{
if(nCode) // 安设HOOK
{
hWndMain=hMainWin;
hKeyHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)HookProc,hInstance,0);
}
else // 卸下HOOK
UnhookWindowsHookEx(hKeyHook);
}
//----------------------------------------------------------------------------------------------------
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
EVENTMSG *keyMSG=(EVENTMSG *)lParam;
if((nCode==HC_ACTION)&&(keyMSG->message==WM_KEYUP))
PostMessage(hWndMain,myMessage,(char)(keyMSG->paramL),SecondPar);
// 向调用窗体发消息myMessage和虚拟键码(char)(keyMSG->paramL)
return((int)CallNextHookEx(hKeyHook,nCode,wParam,lParam));
}
//----------------------------------------------------------------------------------------------------
利用代码以下:(调DLL)
//----------------------------------------------------------------------------------------------------
static HINSTANCE hDLL; // DLL句柄
typedef void __stdcall (*DLLFUN)(HWND,bool);
DLLFUN DLLSetHook;
static const myMessage=2000;
static const SecondPar=1;
//----------------------------------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
{}
//----------------------------------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
hDLL=LoadLibrary((LPCTSTR)"Project1.dll"); // DLL文件名:Project1.dll
if(hDLL==NULL)
{ ShowMessage("DLL: 不能加载!程序退出."); exit(1); }
DLLSetHook =(DLLFUN)GetProcAddress(hDLL,"SetHook");
if(DLLSetHook==NULL)
{ ShowMessage("DLL: 函数没找到!程序退出."); FreeLibrary(hDLL); exit(1); }
DLLSetHook(this->Handle,true);
}
//----------------------------------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
DLLSetHook(NULL,false); // 卸下HOOK
FreeLibrary(hDLL); // 卸下DLL
}
//----------------------------------------------------------------------------------------------------
void __fastcall TForm1::ApplicationEvents1Message(tagMSG &Msg,bool &Handled)
{ // BCB5.0 的ApplicationEvents元件
if((Msg.message==myMessage)&&(Msg.lParam==SecondPar))
ShowMessage(" 收到HOOK按键消息!\n\n 【键虚拟码】:"+IntToStr(Msg.wParam));
}
//----------------------------------------------------------------------------------------------------
用 WH_JOURNALRECORD 范例HOOK可简单实现.
以上是“<b>用键盘全局钩子Hook监督多进程键盘操作</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |