SPY.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:11k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /******************************************************************************
  2. *       This is a part of the Microsoft Source Code Samples. 
  3. *       Copyright (C) 1993-1997 Microsoft Corporation.
  4. *       All rights reserved. 
  5. *       This source code is only intended as a supplement to 
  6. *       Microsoft Development Tools and/or WinHelp documentation.
  7. *       See these sources for detailed information regarding the 
  8. *       Microsoft samples programs.
  9. ******************************************************************************/
  10. /*****************************************************************************
  11. *
  12. * Module: spy.c
  13. *
  14. *   Main module for the Windows debugging Spy SDK applet.
  15. *
  16. * Functions:
  17. *
  18. *   WinMain()
  19. *   SpyWndProc()
  20. *   SpyInit()
  21. *   PutOptions()
  22. *   InitMenu()
  23. *   SpyCommand()
  24. *
  25. * Comments:
  26. *
  27. *****************************************************************************/
  28. #include "spy.h"
  29. #include <stdlib.h>
  30. #define WM_EXECINSTANCE     (WM_USER+100)
  31. /*
  32.  * Macros to simplify working with menus.
  33.  */
  34. #define MyEnableMenuItem(hMenu, wIDEnableItem, fEnable) 
  35.     EnableMenuItem((hMenu),(wIDEnableItem),(fEnable)?MF_ENABLED:MF_GRAYED)
  36. #define MyCheckMenuItem(hMenu, wIDCheckItem, fCheck) 
  37.     CheckMenuItem((hMenu),(wIDCheckItem),(fCheck)?MF_CHECKED:MF_UNCHECKED)
  38. HANDLE ghInst;
  39. HWND ghwndSpyApp;
  40. HWND ghwndPrintf = NULL;
  41. HANDLE ghHookThread = NULL;
  42. HWND ghwndSpyHook = NULL;
  43. HWND ghwndSpyingOn = NULL;              // The window we are spying on.
  44. HFONT ghfontPrintf;
  45. INT gnLines;
  46. BOOL gfSpyOn = FALSE;
  47. BOOL gfSpyAll;
  48. BOOL gfOutputWin;
  49. BOOL gfOutputCom1;
  50. BOOL gfOutputFile;
  51. HFILE gfhFile;
  52. HANDLE gfhCom1;
  53. CHAR gszFile[MAXSTRING];
  54. INT gcxBorder;
  55. INT gcyBorder;
  56. BOOL gfMsgsUser;                        // TRUE to spy on all WM_USER messages.
  57. BOOL gfMsgsUnknown;                     // TRUE to spy on all unknown msgs.
  58. CHAR gszAppName[] = SPYAPPNAME;
  59. UCHAR gszWindowName[40];
  60. WINDOWPLACEMENT gwndpl;
  61. PRIVATE HANDLE ghaccelTbl;              // Accelerator table handle.
  62. PRIVATE CHAR gszSpyClassName[] = SPYCLASSNAME;
  63. PRIVATE BOOL SpyInit(HANDLE hInstance, INT nCmdShow);
  64. PRIVATE VOID PutOptions(VOID);
  65. PRIVATE VOID InitMenu(HMENU hmenu);
  66. PRIVATE LRESULT SpyCommand(HWND hwnd, INT nCmd, INT nNotifyCode);
  67. /*****************************************************************************
  68. * WinMain
  69. *
  70. * Main entry point for the Spy app.
  71. *
  72. *****************************************************************************/
  73. INT WINAPI
  74. WinMain(
  75.     HINSTANCE hInstance,
  76.     HINSTANCE hPrevInstance,
  77.     LPSTR lpCmdLine,
  78.     INT nCmdShow
  79.     )
  80. {
  81.     MSG msg;
  82.     if (!SpyInit(hInstance, nCmdShow))
  83.         return FALSE;
  84.     if (!CreateHookThread())
  85.         goto closespy;
  86.     /*
  87.      * Polling messages from event queue
  88.      */
  89.     while (GetMessage(&msg, NULL, 0, 0))
  90.     {
  91.         if (!TranslateAccelerator(ghwndSpyApp, ghaccelTbl, &msg))
  92.         {
  93.             TranslateMessage(&msg);
  94.             DispatchMessage(&msg);
  95.         }
  96.     }
  97. closespy:
  98.     if (IsWindow(ghwndSpyApp))
  99.     {
  100.         if (DestroyWindow(ghwndSpyApp))
  101.         {
  102.             ghwndSpyApp = NULL;
  103.         }
  104.     }
  105.     if (IsWindow(ghwndPrintf))
  106.     {
  107.         if (DestroyWindow(ghwndPrintf))
  108.         {
  109.             ghwndPrintf = NULL;
  110.         }
  111.     }
  112.     return (INT)msg.wParam;
  113. }
  114. /*****************************************************************************
  115. * SpyInit
  116. *
  117. * Initializes the Spy application.
  118. *
  119. * Arguments:
  120. *   HANDLE hInstance - handle to the instance of SPY.
  121. *   INT nCmdShow - show the window?
  122. *
  123. * Returns:
  124. *   TRUE if successful, FALSE otherwise.
  125. *
  126. *****************************************************************************/
  127. PRIVATE BOOL
  128. SpyInit(
  129.     HANDLE hInstance,
  130.     INT nCmdShow
  131.     )
  132. {
  133.     WNDCLASS wc;
  134.     HWND hwndT;
  135.     CHAR szClassName[40];
  136.     BOOL bFoundPrevSpy = FALSE;
  137.     INT i;
  138.     INT j;
  139.     ghInst = hInstance;
  140.     /*
  141.      * Loop through windows to find one of the spy class.
  142.      */
  143.     for (hwndT = GetWindow(GetDesktopWindow(), GW_CHILD); hwndT;
  144.         hwndT = GetWindow(hwndT, GW_HWNDNEXT))
  145.     {
  146.         if (GetClassName(hwndT, szClassName, 40))
  147.         {
  148.             if (!lstrcmpi(szClassName, gszSpyClassName))
  149.             {
  150.                 bFoundPrevSpy = TRUE;
  151.                 break;
  152.             }
  153.         }
  154.     }
  155.     if (bFoundPrevSpy)
  156.     {
  157.         if (hwndT)
  158.             SendMessage(hwndT, WM_EXECINSTANCE, 0, 0);
  159.         return FALSE;
  160.     }
  161.     if (!(ghaccelTbl = LoadAccelerators(ghInst, "spy")))
  162.         return FALSE;
  163.     ReadRegistry();
  164.     gcxBorder = GetSystemMetrics(SM_CXBORDER);
  165.     gcyBorder = GetSystemMetrics(SM_CYBORDER);
  166.     //
  167.     // Calculate the counts in the message groups.  This is best
  168.     // done at run time to be safe.
  169.     //
  170.     for (i = 0; i < gcMessages; i++)
  171.     {
  172.         //
  173.         // If this message belongs to a message group,
  174.         // increment the total for that group.
  175.         //
  176.         for (j = 0; j < gcMsgGroups; j++)
  177.         {
  178.             if (gaMsgGroup[j].flMask & gaMsgs[i].Flags)
  179.                 gaMsgGroup[j].cMsgs++;
  180.         }
  181.     }
  182.     lstrcpy(gszWindowName,LoadResourceString(IDS_APPLICATION_NAME));
  183.     wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  184.     wc.hIcon          = LoadIcon(hInstance, gszAppName);
  185.     wc.lpszMenuName   = gszAppName;
  186.     wc.lpszClassName  = gszSpyClassName;
  187.     wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  188.     wc.hInstance      = hInstance;
  189.     wc.style          = CS_BYTEALIGNCLIENT;
  190.     wc.lpfnWndProc    = SpyWndProc;
  191.     wc.cbWndExtra     = 0;
  192.     wc.cbClsExtra     = 0;
  193.     if (!RegisterClass(&wc))
  194.         return FALSE;
  195.     ghwndSpyApp = CreateWindow(gszSpyClassName, gszWindowName,
  196.         WS_OVERLAPPEDWINDOW, 0, 0, 0, 0,
  197.         NULL, NULL, hInstance, NULL);
  198.     if (!ghwndSpyApp)
  199.         return FALSE;
  200.     if (nCmdShow != SW_SHOWNORMAL)
  201.         gwndpl.showCmd = nCmdShow;
  202.     SetWindowPlacement(ghwndSpyApp, &gwndpl);
  203.     return TRUE;
  204. }
  205. /*****************************************************************************
  206. * SpyWndProc
  207. *
  208. * Main window procedure for the spy app.
  209. *
  210. * Arguments:
  211. *    HWND hwnd - handle to the spy window
  212. *    UINT msg - message
  213. *    WPARAM wParam - message parameter
  214. *    LPARAM lParam - message parameter
  215. *
  216. * Returns:
  217. *   The value that the window proc should return, based on the processing
  218. *   of the specific WM_COMMAND message received.
  219. *****************************************************************************/
  220. LRESULT CALLBACK
  221. SpyWndProc(
  222.     HWND hwnd,
  223.     UINT msg,
  224.     WPARAM wParam,
  225.     LPARAM lParam
  226.     )
  227. {
  228.     switch (msg)
  229.     {
  230.         case WM_CREATE:
  231.             MyCreatePrintfWin(hwnd);
  232.             return 0;
  233.         case WM_INITMENU:
  234.             if (GetMenu(ghwndSpyApp) == (HMENU)wParam)
  235.                 InitMenu((HMENU)wParam);
  236.             break;
  237.         case WM_COMMAND:
  238.             return SpyCommand(hwnd, LOWORD(wParam), HIWORD(wParam));
  239.         case WM_ACTIVATE:
  240.             /*
  241.              * Set the focus to the printf window if we are being activated.
  242.              */
  243.             if (LOWORD(wParam))
  244.                 SetFocus(ghwndPrintf);
  245.             break;
  246.         case WM_SIZE:
  247.             /*
  248.              * Size the printf window to fit into the new client area size.
  249.              */
  250.             MoveWindow(ghwndPrintf, -gcxBorder, -gcyBorder,
  251.                 LOWORD(lParam) + (2 * gcxBorder),
  252.                 HIWORD(lParam) + (2 * gcyBorder), TRUE);
  253.             break;
  254.         case WM_CLOSE:
  255.             SetSpyHook(FALSE);
  256.             if (gfhCom1 != INVALID_HANDLE_VALUE)
  257.                 CloseHandle(gfhCom1);
  258.             if (gfhFile)
  259.                 _lclose(gfhFile);
  260.             SendMessage(ghwndSpyHook, WM_CLOSE, 0, 0);
  261.             WriteRegistry();
  262.             WaitForSingleObject(ghHookThread, INFINITE);
  263.             DestroyWindow(ghwndSpyApp);
  264.             break;
  265.         case WM_DESTROY:
  266.             PostQuitMessage(0);   /* Kill the main window */
  267.             ghwndSpyApp = NULL;
  268.             ghwndPrintf = NULL;
  269.             break;
  270.         case WM_EXECINSTANCE:
  271.             /*
  272.              * another instance of spy has been started.
  273.              */
  274.             if (IsIconic(hwnd))
  275.                 ShowWindow(hwnd,SW_SHOWNORMAL);
  276.             SetForegroundWindow(hwnd);
  277.             BringWindowToTop(hwnd);
  278.             break;
  279.         default:
  280.             return DefWindowProc(hwnd, msg, wParam, lParam);
  281.     }
  282.     return 0;
  283. }
  284. /*****************************************************************************
  285. * InitMenu
  286. *
  287. * This function grays/enables and checks/unchecks the menu items
  288. * appropriately for the given state.
  289. *
  290. * Arguments:
  291. *   HMENU hmenu - The menu handle.
  292. *
  293. * Returns:
  294. *   VOID
  295. *****************************************************************************/
  296. PRIVATE VOID
  297. InitMenu(
  298.     HMENU hmenu
  299.     )
  300. {
  301.     BOOL fEnable = !IsPrintfEmpty();
  302.     MyEnableMenuItem(hmenu, MENU_EDIT_CUT, fEnable);
  303.     MyEnableMenuItem(hmenu, MENU_EDIT_COPY, fEnable);
  304.     MyEnableMenuItem(hmenu, MENU_EDIT_CLEAR, fEnable);
  305. }
  306. /*****************************************************************************
  307. * SpyCommand
  308. *
  309. * Handles thw WM_COMMAND messages for the Spy app.
  310. *
  311. * Arguments:
  312. *   HWND hwnd       - Window handle of the main app window.
  313. *   INT nCmd        - Command value.
  314. *   INT nNotifyCode - The notify code.
  315. *
  316. * Returns:
  317. *   The value that the window proc should return, based on the processing
  318. *   of the specific WM_COMMAND message received.
  319. *****************************************************************************/
  320. PRIVATE LRESULT
  321. SpyCommand(
  322.     HWND hwnd,
  323.     INT nCmd,
  324.     INT nNotifyCode
  325.     )
  326. {
  327.     HMENU hmenu;
  328.     switch (nCmd)
  329.     {
  330.         case MENU_SPY_SELECTWINDOW:
  331.             MyDialogBox(DID_SELECTWINDOW, SelectWindowDlgProc);
  332.             break;
  333.         case MENU_SPY_ABOUT:
  334.             MyDialogBox(DID_ABOUT, AboutDlgProc);
  335.             break;
  336.         case MENU_SPY_EXIT:
  337.             PostMessage(hwnd, WM_CLOSE, 0, 0);
  338.             break;
  339.         case MENU_EDIT_CUT:
  340.             if (CopyToClipboard())
  341.                 ClearPrintfWindow(ghwndPrintf);
  342.             break;
  343.         case MENU_EDIT_COPY:
  344.             CopyToClipboard();
  345.             break;
  346.         case MENU_EDIT_CLEAR:
  347.             ClearPrintfWindow(ghwndPrintf);
  348.             break;
  349.         case MENU_OPTIONS_MESSAGES:
  350.             MyDialogBox(DID_MESSAGES, MessagesDlgProc);
  351.             break;
  352.         case MENU_OPTIONS_FONT:
  353.             SelectFont();
  354.             break;
  355.         case MENU_OPTIONS_OUTPUT:
  356.             MyDialogBox(DID_OUTPUT, OutputDlgProc);
  357.             break;
  358.         case MENU_START:
  359.             if (SetSpyHook(TRUE))
  360.             {
  361.                 hmenu = GetMenu(hwnd);
  362.                 ModifyMenu(hmenu, MENUPOS_STARTSTOP, MF_BYPOSITION | MF_STRING,
  363.                     MENU_STOP, LoadResourceString(IDS_MENU_STOP));
  364.                 DrawMenuBar(hwnd);
  365.                 SetSpyCaption();
  366.             }
  367.             break;
  368.         case MENU_STOP:
  369.             if (SetSpyHook(FALSE))
  370.             {
  371.                 hmenu = GetMenu(hwnd);
  372.                 ModifyMenu(hmenu, MENUPOS_STARTSTOP, MF_BYPOSITION | MF_STRING,
  373.                     MENU_START, LoadResourceString(IDS_MENU_START));
  374.                 DrawMenuBar(hwnd);
  375.                 SetSpyCaption();
  376.             }
  377.             break;
  378.     }
  379.     return 0;
  380. }