home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / tk4 / spy / queues.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  14.6 KB  |  473 lines

  1. /***************************************************************************\
  2. * queues.c - Spy code to manager the queue list dialog
  3. *
  4. * Created by Microsoft Corporation, 1989
  5. \***************************************************************************/
  6.  
  7. #define    INCL_WINDIALOGS
  8. #define    INCL_WINHEAP
  9. #define    INCL_WININPUT
  10. #define    INCL_WINLISTBOXES
  11. #define    INCL_WINMESSAGEMGR
  12. #define    INCL_WINPOINTERS        /* needed for spy.h */
  13. #define    INCL_WINWINDOWMGR
  14. #include <os2.h>
  15. #include <spyhook.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include "spy.h"
  21.  
  22. #define SIZEOFWND   34
  23. #define MAXMSGBYTES 100
  24.  
  25. /* File Global Variables */
  26. HMQ      hmqDlgDisp;
  27. SHORT   chmqWatched = 0;
  28. SHORT   chmqInList = 0;
  29.  
  30.  
  31.  
  32. /* Function Prototypes */
  33. MRESULT CALLBACK SpyQueuesDlgProc(HWND, USHORT, MPARAM, MPARAM);
  34.  
  35. void    InitQueueList(VOID);
  36. void    DisplayQueueInfoForWindow(HWND, HWND);
  37. void    DisplayQueueInfo(HWND, HMQ);
  38. void    SelectQueueFromText(HWND hwndDlg);
  39. void    BuildQueueWatchList(void);
  40.  
  41.  
  42.  
  43. /***************************************************************************\
  44. * MRESULT CALLBACK SpyQueuesDlgProc(hwnd, msg, mp1, mp2)
  45. *
  46. * The Spy Windows Dialog procedure
  47. \***************************************************************************/
  48. MRESULT CALLBACK SpyQueuesDlgProc(hwnd, msg, mp1, mp2)
  49. HWND hwnd;
  50. USHORT msg;
  51. MPARAM mp1;
  52. MPARAM mp2;
  53. {
  54.     BOOL        fSelect = TRUE;
  55.     SHORT       cQueues;
  56.     HWND        hwndPoint;
  57.     HMQ         hmqDisp;
  58.     HMQ         hmqItem;   /* from handle of list item */
  59.     USHORT      iItemFocus; /* Index to item that has the focus */
  60.  
  61.     switch (msg) {
  62.  
  63.     case WM_INITDLG:
  64.         /* Initialize the dialog items */
  65.         hwndWindowLB = WinWindowFromID(hwnd, DID_WINDOWLIST);
  66.         InitQueueList();
  67.         hmqDlgDisp = NULL;
  68.         fTrackingListBox = TRUE;
  69.         break;
  70.  
  71.     case WM_CHAR:
  72.         /*
  73.          * Handle VK_ENTER and VK_NEWLINE if our Edit control has
  74.          * the focus and it is a keydown
  75.          */
  76.         if (!(SHORT1FROMMP(mp1) & KC_KEYUP) &&
  77.                 (SHORT1FROMMP(mp1) & KC_VIRTUALKEY) &&
  78.                 ( (SHORT2FROMMP(mp2) == VK_ENTER) ||
  79.                   (SHORT2FROMMP(mp2) == VK_NEWLINE) )) {
  80.  
  81.  
  82.             if (WinQueryFocus(HWND_DESKTOP, FALSE) ==
  83.                     WinWindowFromID(hwnd, DID_WHANDLE)) {
  84.                 SelectQueueFromText(hwnd);
  85.                 break;
  86.             }
  87.         }
  88.  
  89.         /* Normaly pass to dialog procedure to handle message */
  90.         return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  91.         break;
  92.  
  93.     case WM_COMMAND:
  94.         switch (SHORT1FROMMP(mp1)) {
  95.         case DID_OK:
  96.             BuildQueueWatchList();
  97.  
  98.         case DID_CANCEL:
  99.             /* Now dismiss the dialog */
  100.             WinDismissDlg(hwnd, SHORT1FROMMP(mp1));
  101.             break;
  102.         case DID_WUNSELALL:
  103.             fSelect = FALSE;
  104.         case DID_WSELALL:
  105.             cQueues = (SHORT) WinSendMsg(hwndWindowLB, LM_QUERYITEMCOUNT,
  106.                 0L, 0L);
  107.  
  108.             fTrackingListBox = FALSE;
  109.             while (cQueues) {
  110.                 /* Loop through all windows, selecting or unselcting all */
  111.                 WinSendMsg(hwndWindowLB, LM_SELECTITEM, (MPARAM)--cQueues,
  112.                     (MPARAM)fSelect);
  113.             }
  114.             fTrackingListBox = TRUE;
  115.             break;
  116.  
  117.         case DID_WSELMOUSE:
  118.             /* Call function to track mouse, returns window handle */
  119.             hwndPoint = HwndSelWinWithMouse(hwnd, DisplayQueueInfoForWindow);
  120.             if (hwndPoint == NULL)
  121.                 break;    /* No window to process */
  122.  
  123.             /*
  124.              * Now find the window in the list, Make the item visible
  125.              * and set the item as selected.
  126.              */
  127.             hmqDisp = (HMQ)WinQueryWindowULong(hwndPoint, QWL_HMQ);
  128.             cQueues = (SHORT) WinSendMsg(hwndWindowLB, LM_QUERYITEMCOUNT,
  129.                     0L, 0L);
  130.  
  131.             while (cQueues) {
  132.                 /*
  133.                  * Loop through all windows until we wind the right
  134.                  * one with the correct window handle
  135.                  */
  136.                 hmqItem = (HMQ)WinSendMsg(hwndWindowLB, LM_QUERYITEMHANDLE,
  137.                         (MPARAM)--cQueues, 0L);
  138.  
  139.                 if (hmqItem == hmqDisp) {
  140.                     /* found the right item, move it to top */
  141.                     WinSendMsg(hwndWindowLB, LM_SETTOPINDEX, (MPARAM)cQueues, 0L);
  142.                     WinSendMsg(hwndWindowLB, LM_SELECTITEM, (MPARAM)cQueues,
  143.                             (MPARAM)TRUE);
  144.                     break;
  145.                 }
  146.             }
  147.             break;
  148.         }
  149.         break;
  150.  
  151.  
  152.     default:
  153.         /*
  154.          * Default is to see if the listbox has changed its focus
  155.          * item number.  If it has, then we want to display the information
  156.          * about the window that the listbox cursor is over.  There is no
  157.          * legal way to do this, One approach appears to temporary set the
  158.          * listbox to be a single selection listbox, then query its selection
  159.          * and set it back into multiple selection mode.
  160.          */
  161.         if (fTrackingListBox && hwndWindowLB != NULL) {
  162.  
  163.             WinSetWindowBits(hwndWindowLB, QWL_STYLE, 0L, LS_MULTIPLESEL);
  164.             iItemFocus = (USHORT)WinSendMsg(hwndWindowLB, LM_QUERYSELECTION,
  165.                     (MPARAM)LIT_FIRST, 0L);
  166.             WinSetWindowBits(hwndWindowLB, QWL_STYLE, LS_MULTIPLESEL,
  167.                     LS_MULTIPLESEL);
  168.  
  169.             if (iItemFocus != iCurItemFocus) {
  170.                 iCurItemFocus = iItemFocus;
  171.                 if (iItemFocus != (USHORT)-1) {
  172.  
  173.                     hmqDisp = (HMQ)WinSendMsg(hwndWindowLB, LM_QUERYITEMHANDLE,
  174.                             (MPARAM)iItemFocus, 0L);
  175.                     DisplayQueueInfo(hwnd, hmqDisp);
  176.                 }
  177.             }
  178.         }
  179.         return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  180.     }
  181.     return 0L;
  182. }
  183.  
  184.  
  185.  
  186.  
  187. /****************************************************************************\
  188. * InitQueueList ()
  189. *
  190. * Builds the list of Queues displayed in the queues dialog
  191. \***************************************************************************/
  192. void InitQueueList()
  193. {
  194.     HENUM       henum;
  195.     char        szClassName[50];    /* Class name of window */
  196.     HWND        hwnd;
  197.     ULONG       ulHMQ;
  198.     PID         pidWindow;
  199.     TID         tidWindow;
  200.     SHORT       item;
  201.     HMQ         *ahmqWatched;
  202.     HMQ         *phmqT;
  203.     SHORT       chmqAlloc;
  204.     SHORT       i;
  205.  
  206.     /*
  207.      * We will enumerate through all of the OBJECT windows looking for
  208.      * windows that have the  WC_QUEUE type.
  209.      */
  210.     if (chmqWatched > 0) {
  211.         ahmqWatched = (HMQ *)WinAllocMem(hHeap, sizeof(HMQ) * chmqWatched);
  212.         chmqAlloc = chmqWatched;
  213.         if (ahmqWatched != NULL)
  214.             chmqWatched = SpyGetQueueList(chmqWatched, ahmqWatched);
  215.     } else
  216.         ahmqWatched = NULL;
  217.  
  218.     henum = WinBeginEnumWindows(HWND_OBJECT);
  219.     if (henum == NULL)
  220.         return;
  221.  
  222.     chmqInList = 0;
  223.     while ((hwnd = WinGetNextWindow(henum)) != NULL) {
  224.         WinLockWindow(hwnd, FALSE);
  225.         WinQueryClassName(hwnd, sizeof(szClassName),
  226.             (PSZ)szClassName);
  227.  
  228.         if (strcmp(szClassName, "#32767") == 0) {
  229.             /*
  230.              * We found an HMQ, add it to our list, Do not add our own
  231.              * HMQ to the list!.
  232.              */
  233.             ulHMQ = (ULONG)WinQueryWindowULong(hwnd, QWL_HMQ);
  234.             if ((HMQ)ulHMQ != hmqSpy) {
  235.                 WinQueryWindowProcess(hwnd, &pidWindow, &tidWindow);
  236.  
  237.                 /* Reuse szClassName */
  238.                 sprintf(szClassName, "0x%04x (PID:%d TID:%d)",
  239.                         (USHORT)ulHMQ, (SHORT)pidWindow, (SHORT)tidWindow);
  240.  
  241.                 item = (USHORT)WinSendMsg(hwndWindowLB, LM_INSERTITEM,
  242.                         (MPARAM)LIT_END, (MPARAM)(PSZ)szClassName);
  243.  
  244.                 /* Set the item handle to the handle of the window */
  245.                 WinSendMsg(hwndWindowLB, LM_SETITEMHANDLE, (MPARAM)item,
  246.                         (MPARAM)ulHMQ);
  247.                 chmqInList++;
  248.  
  249.                 /*
  250.                  * Now see if we should select the item
  251.                  */
  252.                 if (phmqT = ahmqWatched) {
  253.                     for (i = 0; i < chmqWatched; i++) {
  254.                         if ((HMQ)ulHMQ == *phmqT++) {
  255.                             WinSendMsg(hwndWindowLB, LM_SELECTITEM, (MPARAM)item,
  256.                                     (MPARAM)TRUE);
  257.                             break;
  258.                         }
  259.                     }
  260.                 }
  261.             }
  262.         }
  263.  
  264.     }
  265.  
  266.     WinEndEnumWindows(henum);
  267.  
  268.     if (ahmqWatched)
  269.         WinFreeMem(hHeap, (char *)ahmqWatched, chmqAlloc * sizeof(HMQ));
  270. }
  271.  
  272.  
  273.  
  274. /***************************************************************************\
  275. * DisplayQueueInfoForWindow(HWND hwndDialog, HWND hwnd)
  276. *
  277. * Displays the information about the selected window in the dialog
  278. \***************************************************************************/
  279. void DisplayQueueInfoForWindow(hwndDlg, hwndDisp)
  280. HWND            hwndDlg;
  281. HWND            hwndDisp;
  282. {
  283. HMQ             hmqDisp;
  284.  
  285.     /* simply get the HMQ for the window and call display function */
  286.     hmqDisp = (HMQ)WinQueryWindowULong(hwndDisp, QWL_HMQ);
  287.     DisplayQueueInfo(hwndDlg, hmqDisp);
  288. }
  289.  
  290.  
  291.  
  292.  
  293. /***************************************************************************\
  294. * DisplayQueueInfo(HWND hwndDialog, HWND hwnd)
  295. *
  296. * Displays the information about the selected window in the dialog
  297. \***************************************************************************/
  298. void DisplayQueueInfo(hwndDlg, hmqDisp)
  299. HWND            hwndDlg;
  300. HMQ             hmqDisp;
  301. {
  302.     HWND        hwnd;
  303.     HMQ         hmqWindow;
  304.     HENUM       henum;
  305.     char        szTemp[50];
  306.     PID         pidWindow;
  307.     TID         tidWindow;
  308.  
  309.  
  310.     if (hmqDisp != hmqDlgDisp)
  311.     {
  312.         hmqDlgDisp = hmqDisp;
  313.  
  314.         /*
  315.          * See if we can find a top level window for this HMQ
  316.          */
  317.         henum = WinBeginEnumWindows(HWND_DESKTOP);
  318.         if (henum == NULL)
  319.             return;
  320.  
  321.         while ((hwnd = WinGetNextWindow(henum)) != NULL) {
  322.             WinLockWindow(hwnd, FALSE);
  323.             hmqWindow = (HMQ)WinQueryWindowULong(hwnd, QWL_HMQ);
  324.  
  325.             if (hmqWindow == hmqDisp)
  326.                 break;
  327.         }
  328.         WinEndEnumWindows(henum);
  329.  
  330.  
  331.         if (hwnd == NULL) {
  332.             /*
  333.              * No top level windows found look for object window.
  334.              */
  335.             henum = WinBeginEnumWindows(HWND_OBJECT);
  336.             if (henum == NULL)
  337.                 return;
  338.  
  339.             while ((hwnd = WinGetNextWindow(henum)) != NULL) {
  340.                 WinLockWindow(hwnd, FALSE);
  341.                 hmqWindow = (HMQ)WinQueryWindowULong(hwnd, QWL_HMQ);
  342.  
  343.                 if (hmqWindow == hmqDisp)
  344.                     break;
  345.             }
  346.             WinEndEnumWindows(henum);
  347.             if (hwnd == NULL)
  348.                 return;
  349.         }
  350.  
  351.         /* output information to window now */
  352.         sprintf(szTemp, "0x%04x", (SHORT)hmqDisp);
  353.         WinSetDlgItemText(hwndDlg, DID_WHMQ, (PSZ)szTemp);
  354.  
  355.         sprintf(szTemp, "0x%04x", (SHORT)hwnd);
  356.         WinSetDlgItemText(hwndDlg, DID_WHANDLE, (PSZ)szTemp);
  357.  
  358.         /*
  359.          * Warning, we only query the text if the window is not an object
  360.          * window.  If it is an object window, the message queue may not
  361.          * be processing messages, which could hang us
  362.          */
  363.         if (WinIsChild(hwnd, HWND_OBJECT))
  364.             szTemp[0] = '\0';   /* No text available */
  365.         else
  366.             WinQueryWindowText(hwnd, sizeof(szTemp), (PSZ)szTemp);
  367.         WinSetDlgItemText(hwndDlg, DID_WTEXT, (PSZ)szTemp);
  368.  
  369.         WinQueryWindowProcess(hwnd, &pidWindow, &tidWindow);
  370.         sprintf(szTemp, "%d", (SHORT)pidWindow);
  371.         WinSetDlgItemText(hwndDlg, DID_WPID, (PSZ)szTemp);
  372.         sprintf(szTemp, "%d", (SHORT)tidWindow);
  373.         WinSetDlgItemText(hwndDlg, DID_WTID, (PSZ)szTemp);
  374.     }
  375. }
  376.  
  377.  
  378.  
  379. /***************************************************************************\
  380. * void SelectQueueFromText(hwndDlg)
  381. *
  382. *   Updates the text that is displayed in the message text line
  383. \***************************************************************************/
  384. void SelectQueueFromText(hwndDlg)
  385. HWND    hwndDlg;
  386. {
  387.     char    szTemp[80];
  388.     HMQ     hmqSelect;
  389.     SHORT   cItems;
  390.     SHORT   i;
  391.  
  392.  
  393.     /* First get the edit text from the string */
  394.     WinQueryDlgItemText(hwndDlg, DID_WHANDLE, sizeof(szTemp),
  395.             (PSZ)szTemp);
  396.  
  397.     hmqSelect = (HMQ)UConvertStringToNum(szTemp);
  398.  
  399.     cItems = (SHORT)WinSendMsg(hwndWindowLB, LM_QUERYITEMCOUNT,
  400.             0L, 0L);
  401.  
  402.     for (i=0; i < cItems; i++) {
  403.         if ((HMQ)WinSendMsg(hwndWindowLB, LM_QUERYITEMHANDLE,
  404.                 (MPARAM)i, 0L) == hmqSelect)
  405.             break;  /* found it */
  406.     }
  407.  
  408.     if (i < cItems) {
  409.         /*
  410.          * found the hmq, bring to top, and select it
  411.          */
  412.         WinSendMsg(hwndWindowLB, LM_SETTOPINDEX,
  413.                 MPFROMSHORT(i),  (MPARAM)0L);
  414.  
  415.         /* Always set it on */
  416.         WinSendMsg(hwndWindowLB, LM_SELECTITEM,
  417.                 MPFROMSHORT(i), MPFROMSHORT(TRUE));
  418.  
  419.     } else {
  420.         WinAlarm(HWND_DESKTOP, WA_WARNING);
  421.         WinSetDlgItemText(hwndDlg, DID_WHANDLE, (PSZ)"");
  422.     }
  423.  
  424. }
  425.  
  426.  
  427.  
  428.  
  429. /***************************************************************************\
  430. * BuildQueueWatchList()
  431. *
  432. * Updates the list of windows to be watched from the listbox
  433. \***************************************************************************/
  434. void BuildQueueWatchList(void)
  435. {
  436.  
  437.     USHORT  itemPrevious;
  438.     USHORT  item;
  439.  
  440.     HMQ     *ahmqList;
  441.     HMQ     *phmqT;
  442.  
  443.  
  444.     ahmqList = (HMQ *)WinAllocMem(hHeap, sizeof(HMQ) * chmqInList);
  445.     if ((phmqT = ahmqList) == NULL)
  446.         return;     /* Error */
  447.  
  448.  
  449.     /*
  450.      * Simply loop through asking for the next selected item in the
  451.      * list.  Make sure not to overrun our list.
  452.      */
  453.     itemPrevious = (USHORT)LIT_FIRST;
  454.     chmqWatched = 0;
  455.  
  456.     while ((item = (USHORT)WinSendMsg(hwndWindowLB, LM_QUERYSELECTION,
  457.             (MPARAM)itemPrevious, 0L)) != (USHORT)LIT_NONE) {
  458.         /*
  459.          * Get the items handle, which has the value of the window handle
  460.          */
  461.         *phmqT++ = (HMQ)WinSendMsg(hwndWindowLB, LM_QUERYITEMHANDLE,
  462.                 (MPARAM)item, 0L);
  463.         chmqWatched++;
  464.  
  465.         itemPrevious = item;    /* Where to cointinue the search */
  466.     }
  467.  
  468.     SpySetQueueList (chmqWatched, ahmqList);
  469.  
  470.     WinFreeMem(hHeap, (char *)ahmqList, sizeof(HMQ) * chmqInList);
  471. }
  472.  
  473.