Solved

Understanding HOOKS

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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