程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> 用鍵盤全局鉤子Hook監視多進程鍵盤操作

用鍵盤全局鉤子Hook監視多進程鍵盤操作

編輯:vc教程

閒來無事,在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可簡單實現.

  1. 上一頁:
  2. 下一頁:
欄目導航
Copyright © 程式師世界 All Rights Reserved