Solved

Understanding HOOKS

Posted on 2013-05-27
2
242 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

910 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

21 Experts available now in Live!

Get 1:1 Help Now