home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 3_2004-2005.ISO / Data / Zips / VB_API_cod1852392132005.psc / clsSysMonitor.cls < prev    next >
Encoding:
Visual Basic class definition  |  2004-04-22  |  8.2 KB  |  288 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsSysMonitor"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16.  
  17. Public picRAM                     As PictureBox
  18. Public picCPU                     As PictureBox
  19. Public WithEvents Timer           As Timer
  20. Attribute Timer.VB_VarHelpID = -1
  21.  
  22.   
  23. Dim bOnce As Boolean
  24.  
  25. '''''''RAM monitoring variables
  26. Private clrFactor    As Currency
  27. Private TotRam       As Long
  28. Private AvailRam     As Long
  29. Private UsedRam      As Long
  30. ''''''''''''''''''''''''''''''''
  31.  
  32. ''''''CPU monitoring variables
  33. Private PercCPUload  As Long
  34. Private sk As Long
  35. Private HQ As Long  ' handle to Query
  36. Private counter As Long
  37. Private Const lType = 4
  38. Private Const lSize = 4
  39. Private Const VER_PLATFORM_WIN32_NT = 2
  40. Private Const HKEY_DYN_DATA As Long = &H80000006
  41. '''''''''''''''''''''''''''''''
  42.  
  43.  
  44. Event TotSysRam(sVal As String)
  45. Event RamInfo(sAvailRam As String, sUsedram As String)
  46. Event CPUloadInfo(sCpuPercentLoad As String)
  47.  
  48.  
  49. Private Sub Class_Initialize()
  50.  
  51.        bOnce = True
  52. End Sub
  53.  
  54. '----------------------------------------------------------------------
  55. '   INPUTS: | NONE
  56. '  RETURNS: | NONE
  57. ' COMMENTS: | starts the timer
  58. '----------------------------------------------------------------------
  59. Sub StartMonitoring()
  60.  
  61.         Timer.Interval = 1500
  62.         Timer.Enabled = True
  63. End Sub
  64.  
  65.  
  66.  
  67. '----------------------------------------------------------------------
  68. '   INPUTS: | NONE
  69. '  RETURNS: | NONE
  70. ' COMMENTS: | calls the 2 main functions every 1.5 sec that tell us
  71. '             what we want to know about current Ram and CPU load
  72. '----------------------------------------------------------------------
  73. Private Sub Timer_Timer()
  74.        
  75.        'init function is called once to initilize settings
  76.         If bOnce = True Then
  77.              Call InitializeCPUstuff
  78.              Call InitializeRAMstuff
  79.              bOnce = False
  80.         End If
  81.         
  82.        Call GetRam
  83.        DoEvents
  84.        Call GetCpu
  85. End Sub
  86.  
  87. '----------------------------------------------------------------------
  88. '   INPUTS: | NONE
  89. '  RETURNS: | NONE
  90. ' COMMENTS: | updates the status of cpu load every 1.5 seconds
  91. '----------------------------------------------------------------------
  92. Private Sub GetCpu()
  93. Dim lData As Long
  94. Dim hKey As Long
  95.  
  96.        
  97.         If IsOsWinXP Then  '                      if windows xp
  98.              Call PdhCollectQueryData(HQ)
  99.              PercCPUload = CLng(PdhVbGetDoubleCounterValue( _
  100.                   counter, lData))
  101.                   
  102.              RaiseEvent CPUloadInfo(CStr(PercCPUload))
  103.              
  104.         Else  '                                    if os other than xp
  105.              Call RegOpenKey( _
  106.                   HKEY_DYN_DATA, _
  107.                   "PerfStats\StartStat", _
  108.                   hKey)
  109.                   
  110.              Call RegQueryValueEx( _
  111.                   hKey, _
  112.                   "KERNEL\CPUUsage", _
  113.                   0, lType, lData, lSize)
  114.                   
  115.              Call RegCloseKey(hKey)
  116.              
  117.              Call RegOpenKey( _
  118.                   HKEY_DYN_DATA, _
  119.                   "PerfStats\StatData", sk)
  120.      
  121.              Call RegQueryValueEx( _
  122.                   sk, "KERNEL\CPUUsage", _
  123.                   0, lType, lData, lSize)
  124.                   
  125.              PercCPUload = lData
  126.              
  127.              RaiseEvent CPUloadInfo(CLng(PercCPUload))
  128.           End If
  129.  
  130.           Call PaintCpuGraph
  131. End Sub
  132.  
  133. '----------------------------------------------------------------------
  134. '   INPUTS: | NONE
  135. '  RETURNS: | NONE
  136. ' COMMENTS: | updates the status of ram every 1.5 seconds
  137. '----------------------------------------------------------------------
  138. Private Sub GetRam()
  139.     Dim MemStat As MEMORYSTATUSEX
  140.     Dim TotalPhys As Long
  141.     'initialize structure
  142.     MemStat.dwLength = Len(MemStat)
  143.     'retireve memory information
  144.     GlobalMemoryStatusEx MemStat
  145.     'convert large integer to currency
  146.     TotalPhys = LargeIntToCurrency(MemStat.ullTotalPhys)
  147.     'show result
  148.     AvailRam = Format(((MemStat.ullAvailPhys.LowPart / 1024) / 1000))
  149.     TotRam = (TotalPhys / 1024 ^ 2)
  150.     UsedRam = (TotRam - AvailRam)
  151.     RaiseEvent RamInfo(CStr(AvailRam), CStr(UsedRam))
  152.     
  153.     Call PaintRamGraph
  154. End Sub
  155.  
  156.  
  157.  
  158. '----------------------------------------------------------------------
  159. '   INPUTS: | NONE
  160. '  RETURNS: | NONE
  161. ' COMMENTS: | to properly obtain cpu info we need to know OS
  162. '----------------------------------------------------------------------
  163. Private Function IsOsWinXP() As Boolean
  164.  
  165.     Dim vi As OSVERSIONINFO
  166.     vi.dwOSVersionInfoSize = Len(vi)
  167.     Call GetVersionEx(vi)
  168.     IsOsWinXP = (vi.dwPlatformId = VER_PLATFORM_WIN32_NT)
  169.     
  170. End Function
  171.  
  172.  
  173. '----------------------------------------------------------------------
  174. '   INPUTS: | NONE
  175. '  RETURNS: | NONE
  176. ' COMMENTS: | stuff that needs to be processed once
  177. '----------------------------------------------------------------------
  178. Private Sub InitializeCPUstuff()
  179. Dim lData           As Long
  180. Dim hKey            As Long
  181. Dim r               As Long
  182.  
  183.       picCPU.AutoRedraw = True
  184.       picCPU.ScaleWidth = 100 'percent
  185.       
  186.       If IsOsWinXP Then
  187.            Call PdhVbOpenQuery(HQ)
  188.            Call PdhVbAddCounter(HQ, "\Processor(0)\% Processor Time", counter)
  189.         
  190.            Call PdhCollectQueryData(HQ)
  191.            Call PdhVbGetDoubleCounterValue(counter, lData)
  192.       End If
  193. End Sub
  194. '----------------------------------------------------------------------
  195. '   INPUTS: | NONE
  196. '  RETURNS: | NONE
  197. ' COMMENTS: |  stuff that needs to be processed once
  198. '----------------------------------------------------------------------
  199. Private Sub InitializeRAMstuff()
  200.         Call GetRam
  201.         picRAM.ScaleWidth = TotRam
  202.         picRAM.AutoRedraw = True
  203.         'the purpose of this is to create uniform color
  204.         'gradiency in the picturebox regardless of the
  205.         'total amount of ram the user has
  206.         'this variable acts a a picurebox color buffer
  207.         clrFactor = (TotRam / 255)
  208.         
  209.         RaiseEvent TotSysRam(CStr(TotRam))
  210.         picRAM.ToolTipText = "total system RAM: " & TotRam & " mb"
  211.         Timer.Interval = 1500
  212.         Timer.Enabled = True
  213. End Sub
  214.  
  215.  
  216. '----------------------------------------------------------------------
  217. '   INPUTS: | NONE
  218. '  RETURNS: | NONE
  219. ' COMMENTS: | this actually paints the RAM representation in picRam
  220. '----------------------------------------------------------------------
  221. Private Sub PaintRamGraph()
  222.  Dim i                     As Integer
  223.  
  224.  On Error Resume Next
  225.  
  226.        With picRAM
  227.          .Cls
  228.          'picbox scale width is val of used ram
  229.           For i = 0 To UsedRam
  230.             'the more ram thats used, the farther to the right
  231.             'edge of picbox drawing goes, and the less blue
  232.             'and more red show to visually create warning lever
  233.             'on low ram
  234.             picRAM.Line _
  235.                 (i, 0)-(i, .ScaleHeight), _
  236.                 RGB((i * clrFactor), 100, (250 - (i * clrFactor)))
  237.           Next i
  238.        End With
  239. End Sub
  240.  
  241. Private Sub PaintCpuGraph()
  242.  Dim i                     As Integer
  243.  
  244.  On Error Resume Next
  245.  
  246.        With picCPU
  247.          .Cls
  248.          'picbox scale width is val of used ram
  249.           For i = 0 To PercCPUload
  250.             'the more cpu load there is, the farther to the right
  251.             'edge of picbox drawing goes, and the less blue
  252.             'and more red show to visually create warning lever
  253.             'on low ram
  254.             picCPU.Line _
  255.                 (i, 0)-(i, .ScaleHeight), _
  256.                 RGB((i * 2), 100, (250 - (i * 2)))
  257.           Next i
  258.        End With
  259.  
  260. End Sub
  261.  
  262.  
  263. Private Function LargeIntToCurrency(liInput As LARGE_INTEGER) As Currency
  264.     'copy 8 bytes from the large integer to an empty currency
  265.     CopyMemory LargeIntToCurrency, liInput, LenB(liInput)
  266.     'adjust it
  267.     LargeIntToCurrency = LargeIntToCurrency * 10000
  268. End Function
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.