Solved

Understanding HOOKS

Posted on 2013-05-27
2
240 Views
Last Modified: 2013-06-03
I have some code that monitors an Edit control and looks for the number 7 to be entered. When a 7 is entered, an action is taken.

I am new to c++, but want to understand each and every line of this code thoroughly.

To be fair, I will, of course break this down into multiple questions.

My first question about this code is:

[1] Can we go over the GetMsgProc function? A simple line by line explanation would be most beneficial.

Thank you! Although this code does work, I want to understand WHY.



and here is the code:

#include <windows.h>
HHOOK g_hhk;
HWND g_hEdit;

LRESULT CALLBACK GetMsgProc(
    int code,
    WPARAM wParam,
    LPARAM lParam
)
{
    if (0 > code || PM_NOREMOVE == wParam) return CallNextHookEx(g_hhk,code,wParam,lParam);

    MSG* pmsg = (MSG*) lParam;

    if (pmsg->message == WM_KEYDOWN) { // this one is for us

        if (pmsg->hwnd == g_hEdit && pmsg->wParam == VK_7) { // '7' has been pressed if the monitored edit control

            // ---> ACTION!
            MessageBox(NULL, "The number was pressed!", "Notice", MB_OK);
        }
    }

    return CallNextHookEx(g_hhk,code,wParam,lParam);
}

void InitMonitoring(HWND hEdit) {

    g_hEdit = hEdit;

    g_hhk = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,NULL,GetCurrentThreadId());
}

void StopMonitoring() {

    UnhookWindowsHookEx(g_hhk);
}

Open in new window

0
Comment
Question by:edvinson
2 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
ID: 39200367
Hi edvinson,

the GetMsgProc function is called every time the application calls GetMessage or PeekMessage because it's installed as a hook of type WH_GETMESSAGE. To learn more about hooks reading this is a good start: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx

In GetMsgProc first two things are checked:

1. If code is less than 0 the function has to call CallNextHookEx without further processing. This is proposed at http://msdn.microsoft.com/en-us/library/windows/desktop/ms644981%28v=vs.85%29.aspx

2. If wParam is PM_NOREMOVE the function even just calls CallNextHookEx without further processing because this means the application called PeekMessage without removing the message from the message queue. So this avoids the same message being processed more than once.

Next the function checks if the message is a WM_KEYDOWN and the control which recieved the message is the wanted g_hEdit and the pressed key is 7. If so the message box is shown.

In any case next the function calls CallNextHookEx to allow other maybe installed hooks to proccess the message too.

Hope that helps,

ZOPPO
0
 
LVL 1

Author Closing Comment

by:edvinson
ID: 39217995
very thorough explanation thank you very much
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now