home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OProcStat.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  5KB  |  201 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OProcStat.cpp
  5.  
  6.  
  7.  
  8. /*
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  15.  *    endorse or promote products derived from this software
  16.  *    without specific prior written permission.
  17.  * 3. See OCL.INF for a detailed copyright notice.
  18.  *
  19.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29.  * SUCH DAMAGE.
  30.  */
  31.  
  32. // $Header: W:/Projects/OCL/Source/rcs/OProcStat.cpp 1.50 1996/08/11 23:49:29 B.STEIN Release $
  33.  
  34. #define __OCL_SOURCE__
  35.  
  36. #define OINCL_OSTRING
  37. #define OINCL_BASE
  38.  
  39. #include <ocl.hpp>
  40. #include <OProcStat.hpp>
  41. #include <OBuffer.hpp>
  42.  
  43. #define PROCSTAT_PID  1
  44. #define PROCSTAT_PPID 2
  45.  
  46.  
  47. // member funtions of OProcStat
  48.  
  49. OProcStat::OProcStat()
  50.   : ProcCount(0), TaskCount(0)
  51.  {}
  52.  
  53.  
  54. OProcStat::~OProcStat()
  55. {
  56.  ProcList.reset();
  57. }
  58.  
  59.  
  60. PSZ OProcStat::isOfType() const
  61.  return("OProcStat"); 
  62. }
  63.  
  64.  
  65. BOOL OProcStat::getProcesses()
  66. {
  67.  OProcRecord record;
  68.  qsPtrRec_t  *MainIndex;
  69.  OBuffer     buffer(OBuffer::TILESIZE,
  70.                     OBuffer::ALLOC |
  71.                     OBuffer::TILED);
  72.  
  73. // clean up
  74.  
  75.   ProcList.reset();
  76.   ProcCount = 0;
  77.   TaskCount = 0;
  78.  
  79. // Use DosQuerySysState - a new 32bit-API - for Warp and PPC.
  80.  
  81. if(DosQuerySysState(0x17, 0, 0, 0, (PBYTE)buffer, PROCSTAT_BUFFER_SIZE))
  82.   {
  83.    DosFreeMem(buffer);
  84.    return(FALSE); 
  85.   }
  86. else
  87.   {
  88.    qsLrec_t  *module;
  89.    MainIndex = (qsPtrRec_t*) buffer.getPtr();
  90.    module    = MainIndex->pLibRec;     // Start at first module
  91.  
  92.    while(module)
  93.     {
  94.      qsPrec_t *proc = MainIndex->pProcRec;
  95.  
  96.      while((proc) && (proc->RecType != PROCESS_END_INDICATOR))
  97.        {
  98.         if(proc->hMte == module->hmte)       // Module found
  99.          {
  100.           if (!strcmp((PSZ)module->pName, "SYSINIT"))
  101.             record.Module << (PSZ) "MDOS Process";
  102.           else
  103.             record.Module << (PSZ) module->pName;
  104.           record.Pid     = proc->pid;
  105.           record.PPid    = proc->ppid;
  106.           record.Sid     = proc->sgid;
  107.           record.Threads = proc->cTCB;
  108.           ProcList << &record;
  109.           TaskCount++;
  110.          }
  111.         proc = (qsPrec_t *)(proc->pThrdRec + proc->cTCB);       // point to next process
  112.        }
  113.      ProcCount++;
  114.      module = module->pNextRec;  // point to next module
  115.     }
  116.   }
  117.  return(TRUE);
  118. }
  119.  
  120.  
  121.  
  122. ULONG OProcStat::getPid(PSZ module)
  123. {
  124.  return(getNumInfo(module, PROCSTAT_PID));
  125. }
  126.  
  127.  
  128. ULONG OProcStat::getPPid(PSZ module)
  129. {
  130.  return(getNumInfo(module, PROCSTAT_PPID));
  131. }
  132.  
  133.  
  134. ULONG OProcStat::getNumInfo(PSZ module, ULONG flag)
  135. {
  136.  OString                tmpString;
  137.  OListItem<OProcRecord> *tmp;
  138.  
  139.  if (!getProcesses())
  140.    return(0);
  141.  
  142.  tmp = ProcList.first();
  143.  tmpString << module;
  144.  
  145.  while((tmp) && (tmp->item))
  146.   {
  147.    if (tmpString.getText() == tmp->item->Module.getText())
  148.     {
  149.      switch (flag)
  150.       {
  151.        case PROCSTAT_PID:
  152.          return(tmp->item->Pid);
  153.  
  154.        case PROCSTAT_PPID:
  155.          return(tmp->item->PPid);
  156.       }     
  157.     }
  158.    tmp = tmp->next;
  159.   }
  160.  return(0);
  161. }
  162.  
  163.  
  164. PSZ OProcStat::getModule(ULONG pid)
  165. {
  166.  OListItem<OProcRecord> *tmp;
  167.  
  168.  if (!getProcesses())
  169.    return(0);
  170.  
  171.  tmp = ProcList.first();
  172.  
  173.  while((tmp) && (tmp->item))
  174.   {
  175.    if (tmp->item->Pid == pid)
  176.      return(strdup(tmp->item->Module));
  177.    tmp = tmp->next;
  178.   }
  179.  return(NULL);
  180. }
  181.  
  182.  
  183. ULONG OProcStat::getProcCount() 
  184. {
  185.  if (getProcesses())
  186.     return(ProcCount);
  187.  else return(0); 
  188. }
  189.  
  190.  
  191. ULONG OProcStat::getTaskCount() 
  192. {
  193.  if (getProcesses())
  194.     return(TaskCount);
  195.  else return(0); 
  196. }
  197.  
  198.  
  199. // end of source
  200.