home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / msj / v07n03 / toolhelp.exe / LISTS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-01  |  4.1 KB  |  148 lines

  1. /*-------------------------------------------------------------*\
  2.  |  LISTS.C  --  Builds module, task & segment lists for use   |
  3.  |               by Task Master.                               |
  4. \*-------------------------------------------------------------*/
  5. #include <Windows.H>
  6. #include <WindowsX.H>
  7. #include <ToolHelp.H>
  8. #include <Memory.H>
  9. #include "TaskMstr.H"
  10.  
  11.  
  12. /*-------------------------------------------------------------*\
  13.  |                      Global Variables.                      |
  14. \*-------------------------------------------------------------*/
  15. extern HWND                hwndMain;
  16. extern GLOBALENTRY _huge * lpheList;
  17. extern int                 cSegments;
  18. extern int                 cTaskList;
  19. extern LPTASKLIST          lptlList;
  20.  
  21.  
  22. /*-------------------------------------------------------------*\
  23.  |          trBuildSegmentList:  Rebuild Segment List.         |
  24. \*-------------------------------------------------------------*/
  25. void FAR PASCAL trBuildSegmentList()
  26.     {
  27.     BOOL                bOk;
  28.     GLOBALENTRY _huge * lpheTemp;
  29.     GLOBALENTRY         globalentry;
  30.     GLOBALINFO          gi;
  31.     WORD                err;
  32.     WORD                retry;
  33.     WORD                wcItems;
  34.  
  35.     // Init error code.
  36.     err = ERR_OK;
  37.  
  38.     // Query system about number of segments.
  39.     //
  40.     gi.dwSize = sizeof (GLOBALINFO);
  41.     bOk = GlobalInfo (&gi);
  42.  
  43.     if (!bOk)
  44.         {
  45.         err = ERR_GLOBALINFO;
  46.         goto Exit;
  47.         }
  48.  
  49.     // Adjust buffer for segment info.
  50.     //
  51.     wcItems = GlobalSizePtr (lpheList) / sizeof(GLOBALENTRY);
  52.     for (retry = RETRY_SEGALLOC; retry > 0; retry--)
  53.         {
  54.         lpheTemp = (GLOBALENTRY FAR *)GlobalReAllocPtr (lpheList,
  55.                     (SEG_EXTRA + gi.wcItems) * sizeof(GLOBALENTRY),
  56.                      0);
  57.  
  58.         if (!lpheTemp)
  59.             {
  60.             err = ERR_OUTOFMEMORY;
  61.             goto Exit;
  62.             }
  63.         lpheList = lpheTemp;
  64.  
  65.         // Check whether count of items has changed on global heap.
  66.         //
  67.         wcItems = gi.wcItems;
  68.         GlobalInfo (&gi);
  69.         if (wcItems > gi.wcItems)
  70.             break;
  71.         }
  72.  
  73.     cSegments = wcItems;
  74.  
  75.     // Loop through heap, querying segment info.
  76.     //
  77.     globalentry.dwSize = sizeof (GLOBALENTRY);
  78.     GlobalFirst (&globalentry, GLOBAL_ALL);
  79.     _fmemcpy (lpheTemp, &globalentry, sizeof(GLOBALENTRY));
  80.     wcItems--;
  81.  
  82.     for (;wcItems > 0; wcItems--)
  83.         {
  84.         if (GlobalNext (&globalentry, GLOBAL_ALL))
  85.             {
  86.             lpheTemp++;
  87.             _fmemcpy (lpheTemp, &globalentry, sizeof(GLOBALENTRY));
  88.             }
  89.         else
  90.             {
  91.             cSegments -= wcItems;
  92.             break;
  93.             }
  94.         }
  95.  
  96. Exit:
  97.     if (err != ERR_OK)
  98.         {
  99.         PostMessage (hwndMain, AM_ERROR, err, 0);
  100.         }
  101.     }
  102.  
  103.  
  104. /*-------------------------------------------------------------*\
  105.  |             trBuildTaskList:  Build Task List.              |
  106. \*-------------------------------------------------------------*/
  107. void FAR PASCAL trBuildTaskList()
  108.     {
  109.     TASKENTRY  teTemp;
  110.     LPTASKLIST lptlTemp;
  111.     int        cTasks;
  112.  
  113.     if (!lptlList)
  114.         {
  115.         lptlList = (LPTASKLIST)GlobalAllocPtr (GMEM_MOVEABLE,
  116.                                                sizeof(TASKLIST) );
  117.         }
  118.  
  119.     // Count number of tasks in the system.
  120.     //
  121.     cTasks = GetNumTasks();  // Returns invalid value.
  122.     cTaskList = cTasks;
  123.  
  124.     lptlTemp = (LPTASKLIST)GlobalReAllocPtr (lptlList,
  125.                                              cTasks * sizeof(TASKLIST), 0);
  126.     if (!lptlTemp)
  127.         {
  128.         SendMessage (hwndMain, AM_ERROR, ERR_TASKLIST, 0);
  129.         lptlList = (TASKENTRY FAR *)0;
  130.         return;
  131.         }
  132.     lptlList = lptlTemp;
  133.  
  134.     // Fetch list of tasks.
  135.     //
  136.     teTemp.dwSize = sizeof (TASKENTRY);
  137.     TaskFirst (&teTemp);
  138.     _fmemcpy (&lptlTemp->taskentry, &teTemp, sizeof (TASKENTRY));
  139.     cTasks--;
  140.  
  141.     for ( ;cTasks > 0; cTasks--)
  142.         {
  143.         TaskNext(&teTemp);
  144.         lptlTemp++;
  145.         _fmemcpy (&lptlTemp->taskentry, &teTemp, sizeof (TASKENTRY));
  146.         }
  147.     }
  148.