天天看點

hook的簡單示例

//----------------------------------------------------------------------------------------------------

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

下一篇: HOOK專題