Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Please provide MFC SetWindowsHookEx WH_GETMESSAGE Example

Posted on 1999-08-04
4
Medium Priority
?
3,050 Views
Last Modified: 2013-11-20
I have been having terrible difficulties getting my WH_GETMESSAGE hook to work properly as a system hook. I've tried so many different things and fixed so many problems along the way - it seems the road will never end!

I am trying to write an MFC App which installs a WH_GETMESSAGE hook in order to catch WM_PAINT messages that target windows other than my app's.

Please send complete compilable C++ source code for an MFC app and the hook dll which demonstrates installation of a WH_GETMESSAGE hook using SetWindowsHookEx. The code should successfully catch WM_PAINT messages that are destined for other applications.

I've been struggling with this issue for many weeks (on a side-project) and really want it resolved soon, that's why the point value is so high. Any working code is acceptable - I will be reimplementing my own solution.

Thank you,
Brett Levine
0
Comment
Question by:circuit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:speedup
ID: 1312628
codes go here:( references from MSDN )
#define NUMHOOKS 7
 
// Global variables
 
typedef struct _MYHOOKDATA
{
    int nType;
    HOOKPROC hkprc;
    HHOOK hhook;
} MYHOOKDATA;
 
MYHOOKDATA myhookdata[NUMHOOKS];
 
LRESULT WINAPI MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam,
    LPARAM lParam)
{
    static BOOL afHooks[NUMHOOKS];
    int index;
    static HMENU hmenu;
 
    switch (uMsg)
    {
        case WM_CREATE:
 
            // Save the menu handle.
 
            hmenu = GetMenu(hwndMain);
 
            // Initialize structures with hook data. The menu-item
            // identifiers are defined as 0 through 6 in the
            // header file. They can be used to identify array
            // elements both here and during the WM_COMMAND
            // message.
 
            myhookdata[IDM_CALLWNDPROC].nType = WH_CALLWNDPROC;
            myhookdata[IDM_CALLWNDPROC].hkprc = CallWndProc;
            myhookdata[IDM_CBT].nType = WH_CBT;
            myhookdata[IDM_CBT].hkprc = CBTProc;
            myhookdata[IDM_DEBUG].nType = WH_DEBUG;
            myhookdata[IDM_DEBUG].hkprc = DebugProc;
            myhookdata[IDM_GETMESSAGE].nType = WH_GETMESSAGE;
            myhookdata[IDM_GETMESSAGE].hkprc = GetMsgProc;
            myhookdata[IDM_KEYBOARD].nType = WH_KEYBOARD;
            myhookdata[IDM_KEYBOARD].hkprc = KeyboardProc;
            myhookdata[IDM_MOUSE].nType = WH_MOUSE;
            myhookdata[IDM_MOUSE].hkprc = MouseProc;
            myhookdata[IDM_MSGFILTER].nType = WH_MSGFILTER;
            myhookdata[IDM_MSGFILTER].hkprc = MessageProc;
 
            // Initialize all flags in the array to FALSE.
 
            memset(afHooks, FALSE, sizeof(afHooks));
 
            return 0;
 
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                 // The user selected a hook command from the menu.
 
                case IDM_CALLWNDPROC:
                case IDM_CBT:
                case IDM_DEBUG:
                case IDM_GETMESSAGE:
                case IDM_KEYBOARD:
                case IDM_MOUSE:
                case IDM_MSGFILTER:
 
                    // Use the menu-item identifier as an index
                    // into the array of structures with hook data.
 
                    index = LOWORD(wParam);
 
                    // If the selected type of hook procedure isn't
                    // installed yet, install it and check the
                    // associated menu item.
 
                    if (!afHooks[index])
                    {
                        myhookdata[index].hhook = SetWindowsHookEx(
                            myhookdata[index].nType,
                            myhookdata[index].hkprc,
                            (HINSTANCE) NULL, GetCurrentThreadId());
                        CheckMenuItem(hmenu, index,
                            MF_BYCOMMAND | MF_CHECKED);
                        afHooks[index] = TRUE;
                    }
 
                    // If the selected type of hook procedure is
                    // already installed, remove it and remove the
                    // check mark from the associated menu item.
 
                    else
                    {
                        UnhookWindowsHookEx(myhookdata[index].hhook);
                        CheckMenuItem(hmenu, index,
                            MF_BYCOMMAND | MF_UNCHECKED);
                        afHooks[index] = FALSE;
                    }
 
                default:
                    return (DefWindowProc(hwndMain, uMsg, wParam,
                        lParam));
            }
            break;
 
            //
            // Process other messages.
            //
 
        default:
            return DefWindowProc(hwndMain, uMsg, wParam, lParam);
    }
    return NULL;
}
 
/****************************************************************
  WH_CALLWNDPROC hook procedure
 ****************************************************************/
 
LRESULT WINAPI CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    CHAR szCWPBuf[256];
    CHAR szMsg[16];
    HDC hdc;
    static int c = 0;
    int cch;
 
    if (nCode < 0)  // do not process message
        return CallNextHookEx(myhookdata[CALLWNDPROC].hhook, nCode,
                wParam, lParam);
 
    // Call an application-defined function that converts a message
    // constant to a string and copies it to a buffer.
 
    LookUpTheMessage((PMSG) lParam, szMsg);
 
    hdc = GetDC(hwndMain);
 
    switch (nCode)
    {
        case HC_ACTION:
            cch = wsprintf(szCWPBuf,
               "CALLWNDPROC - tsk: %ld, msg: %s, %d times   ",
                wParam, szMsg, c++);
            TextOut(hdc, 2, 15, szCWPBuf, cch);
            break;
 
        default:
            break;
    }
 
    ReleaseDC(hwndMain, hdc);
    return CallNextHookEx(myhookdata[CALLWNDPROC].hhook, nCode,
        wParam, lParam);
}
 
/****************************************************************
  WH_GETMESSAGE hook procedure
 ****************************************************************/
 
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    CHAR szMSGBuf[256];
    CHAR szRem[16];
    CHAR szMsg[16];
    HDC hdc;
    static int c = 0;
    int cch;
 
    if (nCode < 0) // do not process message
        return CallNextHookEx(myhookdata[GETMESSAGE].hhook, nCode,
            wParam, lParam);
 
    switch (nCode)
    {
        case HC_ACTION:
            switch (wParam)
            {
                case PM_REMOVE:
                    lstrcpy(szRem, "PM_REMOVE");
                    break;
 
                case PM_NOREMOVE:
                    lstrcpy(szRem, "PM_NOREMOVE");
                    break;
 
                default:
                    lstrcpy(szRem, "Unknown");
                    break;
            }
 
            // Call an application-defined function that converts a
            // message constant to a string and copies it to a
            // buffer.
 
            LookUpTheMessage((PMSG) lParam, szMsg);
 
            hdc = GetDC(hwndMain);
            cch = wsprintf(szMSGBuf,
                "GETMESSAGE - wParam: %s, msg: %s, %d times   ",
                szRem, szMsg, c++);
            TextOut(hdc, 2, 35, szMSGBuf, cch);
            break;
 
        default:
            break;
    }
 
    ReleaseDC(hwndMain, hdc);
    return CallNextHookEx(myhookdata[GETMESSAGE].hhook, nCode,
        wParam, lParam);
}
 
/****************************************************************
  WH_DEBUG hook procedure
 ****************************************************************/
 
LRESULT CALLBACK DebugProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    CHAR szBuf[128];
    HDC hdc;
    static int c = 0;
    int cch;
 
    if (nCode < 0)  // do not process message
        return CallNextHookEx(myhookdata[DEBUG].hhook, nCode,
            wParam, lParam);
 
    hdc = GetDC(hwndMain);
 
    switch (nCode)
    {
        case HC_ACTION:
            cch = wsprintf(szBuf,
                "DEBUG - nCode: %d, tsk: %ld, %d times   ",
                nCode,wParam, c++);
            TextOut(hdc, 2, 55, szBuf, cch);
            break;
 
        default:
            break;
    }
 
    ReleaseDC(hwndMain, hdc);
    return CallNextHookEx(myhookdata[DEBUG].hhook, nCode, wParam,
        lParam);
}
 
/****************************************************************
  WH_CBT hook procedure
 ****************************************************************/
 
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    CHAR szBuf[128];
    CHAR szCode[128];
    HDC hdc;
    static int c = 0;
    int cch;
 
    if (nCode < 0)  // do not process message
        return CallNextHookEx(myhookdata[CBT].hhook, nCode, wParam,
            lParam);
 
    hdc = GetDC(hwndMain);
 
    switch (nCode)
    {
        case HCBT_ACTIVATE:
            lstrcpy(szCode, "HCBT_ACTIVATE");
            break;
 
        case HCBT_CLICKSKIPPED:
            lstrcpy(szCode, "HCBT_CLICKSKIPPED");
            break;
 
        case HCBT_CREATEWND:
            lstrcpy(szCode, "HCBT_CREATEWND");
            break;
 
        case HCBT_DESTROYWND:
            lstrcpy(szCode, "HCBT_DESTROYWND");
            break;
 
        case HCBT_KEYSKIPPED:
            lstrcpy(szCode, "HCBT_KEYSKIPPED");
            break;
 
        case HCBT_MINMAX:
            lstrcpy(szCode, "HCBT_MINMAX");
            break;
 
        case HCBT_MOVESIZE:
            lstrcpy(szCode, "HCBT_MOVESIZE");
            break;
 
        case HCBT_QS:
            lstrcpy(szCode, "HCBT_QS");
            break;
 
        case HCBT_SETFOCUS:
            lstrcpy(szCode, "HCBT_SETFOCUS");
            break;
 
        case HCBT_SYSCOMMAND:
            lstrcpy(szCode, "HCBT_SYSCOMMAND");
            break;
 
        default:
            lstrcpy(szCode, "Unknown");
            break;
    }
 
    cch = wsprintf(szBuf, "CBT - nCode: %s, tsk: %ld, %d times   ",
        szCode, wParam, c++);
    TextOut(hdc, 2, 75, szBuf, cch);
    ReleaseDC(hwndMain, hdc);
    return CallNextHookEx(myhookdata[CBT].hhook, nCode, wParam,
        lParam);
}
 
/****************************************************************
  WH_MOUSE hook procedure
 ****************************************************************/
 
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    CHAR szBuf[128];
    CHAR szMsg[16];
    HDC hdc;
    static int c = 0;
    int cch;
 
    if (nCode < 0)  // do not process the message
        return CallNextHookEx(myhookdata[MOUSE].hhook, nCode,
            wParam, lParam);
 
    // Call an application-defined function that converts a message
    // constant to a string and copies it to a buffer.
 
    LookUpTheMessage((PMSG) lParam, szMsg);
 
    hdc = GetDC(hwndMain);
    cch = wsprintf(szBuf,
        "MOUSE - nCode: %d, msg: %s, x: %d, y: %d, %d times   ",
        nCode, szMsg, LOWORD(lParam), HIWORD(lParam), c++);
    TextOut(hdc, 2, 95, szBuf, cch);
    ReleaseDC(hwndMain, hdc);
    return CallNextHookEx(myhookdata[MOUSE].hhook, nCode, wParam,
        lParam);
}
 
/****************************************************************
  WH_KEYBOARD hook procedure
 ****************************************************************/
 
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    CHAR szBuf[128];
    HDC hdc;
    static int c = 0;
    int cch;
 
    if (nCode < 0)  // do not process message
        return CallNextHookEx(myhookdata[KEYBOARD].hhook, nCode,
            wParam, lParam);
 
    hdc = GetDC(hwndMain);
    cch = wsprintf(szBuf, "KEYBOARD - nCode: %d, vk: %d, %d times ",
        nCode, wParam, c++);
    TextOut(hdc, 2, 115, szBuf, cch);
    ReleaseDC(hwndMain, hdc);
    return CallNextHookEx(myhookdata[KEYBOARD].hhook, nCode, wParam,
        lParam);
}
 
/****************************************************************
  WH_MSGFILTER hook procedure
 ****************************************************************/
 
LRESULT CALLBACK MessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    CHAR szBuf[128];
    CHAR szMsg[16];
    CHAR szCode[32];
    HDC hdc;
    static int c = 0;
    int cch;
 
    if (nCode < 0)  // do not process message
        return CallNextHookEx(myhookdata[MSGFILTER].hhook, nCode,
            wParam, lParam);
 
    switch (nCode)
    {
        case MSGF_DIALOGBOX:
            lstrcpy(szCode, "MSGF_DIALOGBOX");
            break;
 
        case MSGF_MENU:
            lstrcpy(szCode, "MSGF_MENU");
            break;
 
        case MSGF_SCROLLBAR:
            lstrcpy(szCode, "MSGF_SCROLLBAR");
            break;
 
        default:
            wsprintf(szCode, "Unknown: %d", nCode);
            break;
    }
 
    // Call an application-defined function that converts a message
    // constant to a string and copies it to a buffer.
 
    LookUpTheMessage((PMSG) lParam, szMsg);
 
    hdc = GetDC(hwndMain);
    cch = wsprintf(szBuf,
        "MSGFILTER  nCode: %s, msg: %s, %d times    ",
        szCode, szMsg, c++);
    TextOut(hdc, 2, 135, szBuf, cch);
    ReleaseDC(hwndMain, hdc);
    return CallNextHookEx(myhookdata[MSGFILTER].hhook, nCode,
        wParam, lParam);
}

0
 

Author Comment

by:circuit
ID: 1312629
Yes, I have that example too.  Sorry if I did not make this clear enough:  I am specifically looking for a C++, MFC application which uses a DLL to install a system-wide WH_GETMESSAGE hook.  The code above:  a)  is C code  b) is win32 but not MFC  c) does not build a dll.

Thanks,
Bre++
0
 
LVL 1

Accepted Solution

by:
speedup earned 2000 total points
ID: 1312630
0
 

Author Comment

by:circuit
ID: 1312631
Thanks a lot, that's exactly the example I was looking for.  In just about 20 minutes my code is up and running perfectly.  Codeguru is cool - I wonder how I missed that example (I only saw the one other ones by searching for SetWindowsHookEx).

bre++
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

661 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