Windows Global Hooks

Source: Internet
Author: User
Tags assert getmessage

Recently useful to global hook, actually can refer to Windows core programming, Global hook is also a way of DLL injection.


First, it must be a DLL, you can export an interface, where the global hook is set, the type is wh_getmessage (so that all processes will be hook), so successful, the system will inject this DLL into all processes with GETMESSAGE, The next LoadLibrary is implicitly invoked for each process.

Then every time you getmessage, you can go back to the redeployment callback function and you'll be able to do some of your own processing.


When the hook is not needed, the unhook can be passed so that the system implicitly calls FreeLibrary for the processes on those hooks and unloads the DLL.

We set the hook process for the P,hook DLL to D, then the experiment found that

Even if p does not call D's unhook before exiting, or p is strongly killed or hung, the system will still find that it will still help call freelibrary for those hooks, which will go into the Proc_detach of D.

However, there are some surprises, and occasionally some processes do not unload D, such as Chrome, Svncache, or other processes that take some time to unload (but the 64-bit system seems to be okay?). )。

Static hhook _myhook = null; lresult callback myhookproc (                               _In_  int nCode,                               _In_  WPARAM wParam,                               _In_  LPARAM lParam                               ) {    return callnexthookex (Null, ncode, wparam,  lparam);// hhook&nBsp;parameter is optional and ignored}bool hookstart () {    if   (_myhook)         return true;    _ Myhook = setwindowshookex (        WH_GETMESSAGE,          (HOOKPROC) &MyHookProc,         Getmaindll (),         0);    if  (NULL ==  _myhook)     {        assert (false & &  "[hookstart]hook false!");         return false;    }     return true;} Bool hookend () {    if  (Null == _myhook)          return true;    if (False == unhookwindowshookex (_myhook))     {         assert (false &&  "[hookend]unhook false!");         return false;    }     _myhook = null;    return true;}



This article is from the "V" blog, be sure to keep this source http://4651077.blog.51cto.com/4641077/1613836

Windows Global Hooks

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: info-contact@alibabacloud.com 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.