hook的簡單樣本

來源:互聯網
上載者:User

//----------------------------------------------------------------------------------------------------
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可簡單實現.

 

轉自http://www.qqgb.com/Program/CJJ/Cjjsystem/Program_55981_2.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.