Simple implementation [BCB5]: Keyboard Global Hook [HOOK] to monitor multi-process keyboard operations.

zhaozj2021-02-17  53

I have nothing to do, use BCB5 with BCB5 in Win2K, monitor the global key case. Hook placed and callback functions in a separate DLL, DLL original code is as follows:

/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- --Extern "C" __declspec (dllexport) void __stdcall sethook (HWND, BOOL); LRESULT CALLBACK HOOKPROC (Int Ncode, WPARAM WPARAM, LPARAM LPARAM)

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

Static hinstance hinstance; // Application instance handle static hwnd hwndmain; // mainform handle static hHOOK HKEYHOK; / / HOOK handle static const mymessage = 2000; // Custom message number static const secondpar = 1; // Custom Message 2nd parameter

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

Int WinAPI DLLENTRYPOINT (Hinstance Hinst, Unsigned Long Reason) {hinstance = Hinst; Return 1;

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

void __stdcall SetHook (HWND hMainWin, bool nCode) {if (nCode) // placed HOOK {hWndMain = hMainWin; hKeyHook = SetWindowsHookEx (WH_JOURNALRECORD, (HOOKPROC) HookProc, hInstance, 0);} else // Remove 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; // To call Forms MYMESSAGE and Virtual Keychot (Keymsg-> paraml) Return ((int) CallNexthooKex (HKEYHOK, NCODE, WPARAM, LPARAM) }

/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- - Application code is as follows: (tune DLL)

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

Static Hinstance HDLL; // DLL handle 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 file name: project1.dll if (HDLL == null) {showMessage ("DLL: Can't load Exmitition. "); Exit (1);} DllSethook = (DLLFUN) GetProcaddress (HDLL," Sthook "); if (DllSethook == NULL) {showMessage (" DLL: Function is not found! Program exit. "); Freelibrary (HDLL); exit (1);} DllSethook (this-> handle, true);

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

Void __fastcall tform1 :: formclose (Tclosection & Action) {DllSethook (null, false); // Remove Hook Freelibrary (HDLL); // Remove DLL}

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

void __fastcall TForm1 :: ApplicationEvents1Message (tagMSG & Msg, bool & Handled) {// BCB5.0 of ApplicationEvents element if ((Msg.message == myMessage) && (Msg.lParam == SecondPar)) ShowMessage ( "PUSH message received HOOK ! / N / n 【虚 码 码】: " INTSTOSTR (msg.wparam);} // -------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------

Use the wh_journalrecord type hook to be simple to implement.

转载请注明原文地址:https://www.9cbs.com/read-28619.html

New Post(0)