許湞翔 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://---------------------------------------------------------------------
------