Link to home
Start Free TrialLog in
Avatar of edvinson
edvinsonFlag for United States of America

asked on

Understanding HOOKS

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

ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edvinson

ASKER

very thorough explanation thank you very much