Windows global hook DLL (keyboard)

Source: Internet
Author: User

// 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/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.