// Dllhookkb. CPP: defines the entry point for the DLL application. <br/> // <br/> # include "stdafx. H "<br/> # include" dllhookkb. H "</P> <p> # ifdef _ managed <br/> # pragma managed (push, off) <br/> # endif <br/> # pragma data_seg ("ycishared2010") <br/> hwnd g_hwndcaller = NULL; <br/> hhook g_hhook = NULL; <br/> # pragma data_seg () <br/> bool apientry dllmain (hmodule, <br/> DWORD ul_reason_for_call, <br/> lp Void lpreserved <br/>) <br/>{< br/> switch (ul_reason_for_call) <br/>{< br/> case dll_process_attach: <br/> case dll_thread_attach: <br/> case dll_thread_detach: <br/> case dll_process_detach: <br/> break; <br/>}< br/> return true; <br/>}< br/> # ifdef _ managed <br/> # pragma managed (POP) <br/> # endif <br/> // This is an example of an exported variable <br/> dllhookkb_api int ndllhookkb = 0; <br/> // This is Example of an exported function. <br/> dllhookkb_api int fndllhookkb (void) <br/>{< br/> return 42; <br/>}< br/> // This is the constructor of a class that has been exported. <br/> // see dllhookkb. h for the class definition <br/> cdllhookkb: cdllhookkb () <br/>{< br/> return; <br/>}< br/> // function used to obtain the module handle from the memory address <br/> hmodule winapi modulefromaddress (pvoid PV) <br/>{< br/> memory_basic_information MBI; <br/> If (:: Virtualquery (PV, & MBI, sizeof (MBI ))! = 0) <br/>{< br/> return (hmodule) MBI. allocationbase; <br/>}< br/> else <br/>{< br/> return NULL; <br/>}< br/> // keyboard hook function <br/> lresult callback keyhookproc (INT ncode, wparam, lparam) <br/>{< br/> If (ncode <0 | ncode = hc_noremove) <br/>{< br/> return: callnexthookex (g_hhook, ncode, wparam, lparam); <br/>}< br/> If (lparam & 0x40000000) // send duplicate messages to the next hook chain <br/>{< br/> return: callnext Hookex (g_hhook, ncode, wparam, lparam); <br/>}< br/> // notification main window. The wparam parameter is a virtual key code, the lparam parameter contains information about this key <br/>: postmessage (g_hwndcaller, hm_key, wparam, lparam); </P> <p> return: callnexthookex (g_hhook, ncode, wparam, lparam); <br/>}< br/> // install and uninstall hook functions <br/> dllhookkb_api bool winapi setkeyhook (bool binstall, DWORD dwthreadid, hwnd hwndcaller) <br/>{< br/> bool Bok; </P> <p> g_hwndcaller = hwndcaller; <br/> If (binstal L) <br/>{< br/> g_hhook =: setwindowshookex (wh_keyboard, keyhookproc, modulefromaddress (keyhookproc), dwthreadid); <br/> Bok = (g_hhook! = NULL); <br/>}< br/> else <br/>{< br/> Bok =: unhookwindowshookex (g_hhook); <br/> g_hhook = NULL; <br/>}< br/> return Bok; <br/>}< br/>