Global hooks that can work independently of DLL

Source: Internet
Author: User
Tags 04x
Global hooks that can work independently of DLL

I don't know what you started to understand hook, I have read Jeffrey Richter's "Windows Advanced Programming Guide" (the new Chinese version is translated as "Windows core programming"). In this book, the author introduces three methods to inject code into other processes, one of which is the global message hook. I first learned about global hooks from this book.
We should all know that the global message hook depends on a DLL to work properly. As a result, I think that global hooks depend on a DLL to work normally. I think most people must think the same way as me.
But this is not the case. Some global hooks can work normally without any DLL. These hooks include wh_journalplayback, wh_journalrecord, wh_keyboard_ll, and wh_mouse_ll. Why can these hooks work properly without relying on the DLL? We can get the answer from msdn, which describes these four hooks as "this hook is called in the context of the thread that installed it. the hook function is called in the context of the thread where the hook is installed.
To be more clear, it means that these hooks are installed in the thread where they are executed. Therefore, the use of these four hooks does not achieve the effect of code injection, of course, it can be independent of any DLL. In msdn, only some hooks are pointed out that dll must still be used.
The following is an example of the code for the underlying keyboard hook. Of course, DLL is not required.

# DEFINE _ win32_winnt 0400

DWORD g_main_tid = 0;
Hhook g_kb_hook = 0;

Bool callback con_handler (DWORD)
{
Postthreadmessage (g_main_tid, wm_quit, 0, 0 );
Return true;
};

Lresult callback kb_proc (INT code, wparam W, lparam L)
{
Pkbdllhookstruct P = (pkbdllhookstruct) L;
Const char * info = NULL;
If (W = wm_keydown)
Info = "Key DN ";
Else if (W = wm_keyup)
Info = "key up ";
Else if (W = wm_syskeydown)
Info = "sys key DN ";
Else if (W = wm_syskeyup)
Info = "sys key up ";
Printf ("% s-vkcode [% 04x], scancode [% 04x]/n ",
Info, p-> vkcode, p-> scancode );
// Always call next hook
Return callnexthookex (g_kb_hook, code, W, L );
};

Int main (void)
{
G_main_tid = getcurrentthreadid ();
Setconsolectrlhandler (& con_handler, true );
G_kb_hook = setwindowshookex (
Wh_keyboard_ll,
& Kb_proc,
Getmodulehandle (null), // cannot be null, otherwise it fails
0 );
If (g_kb_hook = NULL)
{
Fprintf (stderr,
"Setwindowshookex failed with error % d/N ",
: Getlasterror ());
Return 0;
};
// Message loop is required. You can check msdn for the reason.
MSG;
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
};
Unhookwindowshookex (g_kb_hook );
Return 0;
};

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.