Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[bcb]請教Hook

0 views
Skip to first unread message

Neptune Hsu.

unread,
Jan 28, 2000, 3:00:00 AM1/28/00
to
Hi..
 各位好,我寫了一個小小的Hook程式,DLL及Demo
程式碼如下,我能確定HOOK成功,因為按下ESC後會
有Beep聲,但卻不能順利的傳出訊息給Demo,有時可
以有時不行,不知道我的盲點在那?麻煩先進指導一
下,謝謝。

許湞翔 Neptune Hsu.

-----Hook DLL Source Code -----
HHOOK Hook=NULL;
HWND gHwnd;

extern "C" __declspec(dllexport) bool __stdcall EnableHook(HWND);
extern "C" __declspec(dllexport) bool __stdcall DisableHook();

file://---------------------------------------------------------------------
------
LRESULT CALLBACK KeyboardHookHandle(int iCode, WPARAM wParam, LPARAM lParam)
{
if (iCode >=0)
{
if (wParam==VK_ESCAPE)
{
MessageBeep(1000);
SetActiveWindow(gHwnd);
PostMessage(gHwnd,WM_USER, wParam, lParam);
}
return 0;
}
else return CallNextHookEx(Hook, iCode, wParam, lParam);
}
file://---------------------------------------------------------------------
------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}
file://---------------------------------------------------------------------
------
bool __stdcall EnableHook(HWND hWnd)
{
if(Hook == NULL)
{
gHwnd=hWnd;
Hook = SetWindowsHookEx(WH_KEYBOARD,
(HOOKPROC)KeyboardHookHandle,
HInstance,NULL);
return true;
}
else
return false;
}
file://---------------------------------------------------------------------
------
bool __stdcall DisableHook()
{
if (Hook != NULL)
{
UnhookWindowsHookEx(Hook);
ShowMessage("KeyBoard Hook Disable...!!");
Hook=NULL;
return true;
}
else return false;
}
file://---------------------------------------------------------------------
------

----Demo Application Source Code--------

extern "C" __declspec(dllimport) bool __stdcall EnableHook(HWND);
extern "C" __declspec(dllimport) bool __stdcall DisableHook();

TForm1 *Form1;
file://---------------------------------------------------------------------
------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
file://---------------------------------------------------------------------
------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
EnableHook(Form1->Handle);
}
file://---------------------------------------------------------------------
------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
DisableHook();
}
file://---------------------------------------------------------------------
------

void __fastcall TForm1::AppMessage(tagMSG &Msg, bool
&Handled)
{
if (Msg.message == WM_USER)
{
Memo1->Lines->Add("Key = "+IntToStr(Msg.wParam));
Handled = true;
}
}
file://---------------------------------------------------------------------
------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Application->OnMessage = AppMessage;
}
file://---------------------------------------------------------------------
------

0 new messages