home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 18.ddi / SAMPLES / HOOKS / HOOKS.C_ / HOOKS.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  9.5 KB  |  333 lines

  1. //---------------------------------------------------------------------------
  2. //  Windows hooks Sample Application
  3. //
  4. //  This sample application demonstrates how to use Windows hooks.
  5. //
  6. //  Author:    Kyle Marsh
  7. //              Windows Developer Technology Group
  8. //              Microsoft Corp.
  9. //
  10. //---------------------------------------------------------------------------
  11.  
  12.  
  13. #include "windows.h"
  14. #include "string.h"
  15. #include "hooks.h"
  16.  
  17. //---------------------------------------------------------------------------
  18. // Function declarations
  19. //---------------------------------------------------------------------------
  20.  
  21. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR lpstrCmdLine, int cmdShow);
  22. LONG __export CALLBACK HookSampleWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  23. BOOL __export CALLBACK About(HWND hDlg, unsigned message, WORD wParam, LONG lParam);
  24. int  __export CALLBACK MsgFilterFunc (int nCode, WORD wParam, DWORD lParam );
  25. char FAR *szMessageString(WORD wID);
  26.  
  27. //---------------------------------------------------------------------------
  28. // Global Variables...
  29. //---------------------------------------------------------------------------
  30.  
  31. HINSTANCE hInstance;          // Global instance handle for application
  32. HWND    hwndMain;        // Main hwnd.  Needed in callback
  33. int    nLineHeight;        // Heigth of lines in window
  34.  
  35. //
  36. // Filter Function Addresses
  37. //
  38. FARPROC lpfnMsgFilterProc;
  39.  
  40. //
  41. // Previous Hook Filter Function Pointers
  42. //
  43. HHOOK hhookMsgFilterHook;
  44.  
  45. //
  46. // Output Lines
  47. //
  48. char szMsgFilterLine[128];
  49.  
  50. //---------------------------------------------------------------------------
  51. // WinMain
  52. //---------------------------------------------------------------------------
  53.  
  54. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR lpstrCmdLine, int cmdShow)
  55. {
  56.     MSG        msgMain;
  57.     WNDCLASS   wc;
  58.     HDC        hDC;
  59.     TEXTMETRIC tm;
  60.  
  61.     //
  62.     // Set the global instance variable
  63.     //
  64.     hInstance = hInst;
  65.  
  66.     //
  67.     // Register the window class if this is the first instance.
  68.     //
  69.     if (hInstPrev == NULL)
  70.     {
  71.     wc.lpszMenuName     = "HookSampleMenu";
  72.     wc.lpszClassName    = "HookSampleApp";
  73.         wc.hInstance        = hInst;
  74.     wc.hIcon        = LoadIcon(hInst, "HooksIcon");
  75.         wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  76.     wc.hbrBackground    = (HBRUSH)COLOR_WINDOW + 1;
  77.         wc.style            = 0;
  78.     wc.lpfnWndProc        = HookSampleWndProc;
  79.         wc.cbClsExtra       = 0;
  80.         wc.cbWndExtra       = 0;
  81.  
  82.         if (!RegisterClass(&wc))
  83.             return(0);
  84.     }
  85.  
  86.     //
  87.     // Create the main window
  88.     //
  89.     if ((hwndMain = CreateWindow("HookSampleApp",
  90.                  "Sample Hook Application",
  91.                                  WS_OVERLAPPEDWINDOW,
  92.                                  CW_USEDEFAULT, 0,
  93.                                  CW_USEDEFAULT, CW_USEDEFAULT,
  94.                                  NULL, NULL, hInst, NULL)) == NULL)
  95.         return(0);
  96.  
  97.     //
  98.     // Show the window and make sure it is updated.
  99.     //
  100.     ShowWindow(hwndMain, cmdShow);
  101.     UpdateWindow(hwndMain);
  102.  
  103.     //
  104.     // Work Out The height of lines
  105.     //
  106.     hDC = GetDC(hwndMain);
  107.     GetTextMetrics(hDC, &tm);
  108.     ReleaseDC(hwndMain, hDC);
  109.     nLineHeight = tm.tmHeight+tm.tmExternalLeading;
  110.  
  111.     //
  112.     // Let's pass the hwndMain to the DLL so it can update the Window
  113.     //
  114.     InitHooksDll(hwndMain, nLineHeight);
  115.  
  116.     //
  117.     // Main message "pump"
  118.     //
  119.     while (GetMessage((LPMSG) &msgMain, NULL, 0, 0))
  120.     {
  121.        TranslateMessage((LPMSG) &msgMain);
  122.        DispatchMessage((LPMSG) &msgMain);
  123.     }
  124.  
  125.  
  126.     return(0);
  127. }
  128.  
  129.  
  130.  
  131. //---------------------------------------------------------------------------
  132. // HookSampleWndProc
  133. //
  134. // Window procedure for the sample application's window.
  135. //
  136. //---------------------------------------------------------------------------
  137.  
  138. LONG __export CALLBACK HookSampleWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  139. {
  140.     HMENU       hMenu;
  141.     PAINTSTRUCT ps;
  142.     HDC     hDC;
  143.     int     nHookIndex;
  144.     int     i,State;
  145.  
  146.     switch(msg) {
  147.  
  148.     //
  149.         // Handle menu selections
  150.     //
  151.         case WM_COMMAND:
  152.             switch (wParam) {
  153.  
  154.         case IDM_MSGFILTER:
  155.             //
  156.             // Handle Application Message Filter
  157.                     // Change the checkmark to the proper item
  158.             //
  159.             hMenu = GetMenu(hwnd);
  160.             State = CheckMenuItem(hMenu, wParam, MF_UNCHECKED | MF_BYCOMMAND);
  161.             if ( State == MF_CHECKED ) {
  162.                UnhookWindowsHookEx(hhookMsgFilterHook);
  163.                FreeProcInstance(lpfnMsgFilterProc);
  164.                strcpy(szMsgFilterLine,"                                                                   ");
  165.                hDC = GetDC(hwndMain);
  166.                TabbedTextOut(hDC, 1,nLineHeight * MSGFILTERINDEX,
  167.                 (LPSTR)szMsgFilterLine, strlen(szMsgFilterLine), 0, NULL, 1);
  168.                ReleaseDC(hwndMain, hDC);
  169.             }
  170.             else {
  171.                CheckMenuItem(hMenu, wParam, MF_CHECKED | MF_BYCOMMAND);
  172.                //
  173.                // Install the Hook
  174.                //
  175.                lpfnMsgFilterProc = MakeProcInstance((FARPROC)MsgFilterFunc, hInstance);
  176.                hhookMsgFilterHook = SetWindowsHookEx(WH_MSGFILTER, (HOOKPROC)lpfnMsgFilterProc,hInstance,NULL);
  177.             }
  178.             break;
  179.  
  180.         case IDM_CALLWNDPROC:
  181.         case IDM_CBT:
  182.         case IDM_GETMESSAGE:
  183.         case IDM_JOURNALPLAYBACK:
  184.         case IDM_JOURNALRECORD:
  185.         case IDM_KEYBOARD:
  186.         case IDM_MOUSE:
  187.         case IDM_SYSMSGFILTER:
  188.             //
  189.             // Handle the System Wide Filter
  190.                     // Change the checkmark to the proper item
  191.             //
  192.             nHookIndex = wParam - IDM_CALLWNDPROC;
  193.             hMenu = GetMenu(hwnd);
  194.             State = CheckMenuItem(hMenu, wParam, MF_UNCHECKED | MF_BYCOMMAND);
  195.             if ( State == MF_CHECKED ) {
  196.                InstallFilter(nHookIndex, FALSE);
  197.                if (wParam == IDM_JOURNALRECORD )
  198.               EnableMenuItem(hMenu, IDM_JOURNALPLAYBACK, MF_ENABLED | MF_BYCOMMAND);
  199.             }
  200.             else {
  201.                CheckMenuItem(hMenu, wParam, MF_CHECKED | MF_BYCOMMAND);
  202.                //
  203.                // Install the Hook
  204.                //
  205.                InstallFilter(nHookIndex, TRUE);
  206.             }
  207.             break;
  208.  
  209.         case IDM_ABOUT:
  210.             //
  211.             // Display About Box
  212.             //
  213.             DialogBox(hInstance,         // current instance
  214.             "AboutBox",                       // resource to use
  215.             hwndMain,                     // parent handle
  216.             (DLGPROC)About);            // About() instance address
  217.  
  218.             break;
  219.  
  220.             }
  221.         break;
  222.  
  223.     case WM_PAINT:
  224.         hDC = BeginPaint(hwndMain, &ps);
  225.         TabbedTextOut(hDC, 1,nLineHeight * MSGFILTERINDEX,
  226.               (LPSTR)szMsgFilterLine, strlen(szMsgFilterLine), 0, NULL, 1);
  227.         PaintHooksDll( hDC );
  228.         EndPaint(hwndMain, &ps);
  229.             break;
  230.  
  231.         case WM_DESTROY:
  232.         //
  233.         // Clean up all the Hooks Left
  234.         //
  235.         hMenu = GetMenu(hwndMain);
  236.         for (i = 0; i < NUMOFHOOKS; i++ ) {
  237.         State = CheckMenuItem(hMenu, i+IDM_CALLWNDPROC, MF_UNCHECKED | MF_BYCOMMAND);
  238.         if ( State )
  239.            if ( i == MSGFILTERINDEX ) {
  240.                UnhookWindowsHook(WH_MSGFILTER, (HOOKPROC)lpfnMsgFilterProc);
  241.                FreeProcInstance(lpfnMsgFilterProc);
  242.            }
  243.            else
  244.                InstallFilter(i, FALSE);
  245.         }
  246.             PostQuitMessage(0);
  247.             break;
  248.  
  249.         default:
  250.             return(DefWindowProc(hwnd, msg, wParam, lParam));
  251.     }
  252.  
  253.     return(0);
  254. }
  255.  
  256.  
  257.  
  258. //---------------------------------------------------------------------------
  259. // About
  260. //
  261. // Dialog procedure for the sample application's about box
  262. //
  263. //---------------------------------------------------------------------------
  264. BOOL __export CALLBACK About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  265. {
  266.     switch (message) {
  267.     case WM_INITDIALOG:          // message: initialize dialog box
  268.         return (TRUE);
  269.  
  270.     case WM_COMMAND:          // message: received a command
  271.         if (wParam == IDOK          // "OK" box selected?
  272.         || wParam == IDCANCEL) {  // System menu close command?
  273.         EndDialog(hDlg, TRUE);      // Exits the dialog box
  274.         return (TRUE);
  275.         }
  276.         break;
  277.     }
  278.     return (FALSE);              // Didn't process a message
  279. }
  280.  
  281.  
  282.  
  283. //---------------------------------------------------------------------------
  284. // MsgFilterFunc
  285. //
  286. // Filter function fot the WH_MSGFILTER -- Task Specific so not in DLL
  287. //
  288. //---------------------------------------------------------------------------
  289. int __export CALLBACK MsgFilterFunc (int nCode, WORD wParam, DWORD lParam )
  290. {
  291.    char szType[16];
  292.    MSG FAR *lpMsg;
  293.    HDC           hDC;
  294.  
  295.    if ( nCode >= 0 ) {
  296.       if ( nCode == MSGF_DIALOGBOX )
  297.      strcpy(szType,"Dialog");
  298.       else
  299.      strcpy(szType,"Menu");
  300.  
  301.       lpMsg = (MSG FAR *) lParam;
  302.       wsprintf((LPSTR)szMsgFilterLine,
  303.            "MSGFILTER\t%s\tWnd:%d Time:%ld  Point:%d %d %s            ",
  304.            (LPSTR)szType, lpMsg->hwnd, lpMsg->time,
  305.            lpMsg->pt.x, lpMsg->pt.y, szMessageString(lpMsg->message));
  306.  
  307.       hDC = GetDC(hwndMain);
  308.       TabbedTextOut(hDC, 1,nLineHeight * MSGFILTERINDEX,
  309.            (LPSTR)szMsgFilterLine, strlen(szMsgFilterLine), 0, NULL, 1);
  310.       ReleaseDC(hwndMain, hDC);
  311.    }
  312.  
  313.    return( (int)CallNextHookEx(hhookMsgFilterHook, nCode, wParam, lParam ) );
  314. }
  315.  
  316.  
  317. //---------------------------------------------------------------------------
  318. // MessageString
  319. //
  320. // Function to load string from the STRINGTABLE
  321. //
  322. //---------------------------------------------------------------------------
  323. char FAR *szMessageString(WORD wID)
  324. {
  325.    static char szBuffer[256];
  326.  
  327.    if ( LoadString(hInstance, wID, szBuffer, 255) == 0)
  328.       wsprintf(szBuffer,"Message:%hH",wID);
  329.  
  330.    return (szBuffer);
  331.  
  332. }
  333.