[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Understanding HOOKS

Posted on 2013-05-27
2
Medium Priority
?
263 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 31

Accepted Solution

by:
Zoppo earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

872 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