home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / SysMon_wit2155896252009.psc / ClassPMonRAM.cls < prev    next >
Text File  |  2009-06-25  |  12KB  |  336 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 = "ClassPMonRam"
  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. Private Const HKEY_LOCAL_MACHINE = &H80000002
  17. Private Const HKEY_PERFORMANCE_DATA = &H80000004
  18. Private Const ERROR_SUCCESS = 0&
  19. Private Const ERROR_MORE_DATA = 234
  20.  
  21. Private Declare Function RegConnectRegistry Lib "advapi32.dll" Alias "RegConnectRegistryA" (ByVal lpMachineName As String, ByVal hKey As Long, phkResult As Long) As Long
  22. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  23.  
  24. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  25. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  26. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
  27. Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
  28.  
  29.  
  30. Private Declare Sub Memcopy Lib "KERNEL32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
  31. '//*************************************************************
  32. Private Type FILETIME
  33.     dwLowDateTime As Long
  34.     dwHighDateTime As Long
  35. End Type
  36.  
  37. Private Type SystemTime
  38.     wYear As Integer
  39.     wMonth As Integer
  40.     wDayOfWeek As Integer
  41.     wDay As Integer
  42.     wHour As Integer
  43.     wMinute As Integer
  44.     wSecond As Integer
  45.     wMilliseconds As Integer
  46. End Type
  47.  
  48. Private Type LARGE_INTEGER
  49.     lowpart As Long
  50.     highpart As Long
  51. End Type
  52.  
  53. Private Type PERF_INSTANCE_DEFINITION
  54.     ByteLength As Long
  55.     ParentObjectTitleIndex As Long
  56.     ParentObjectInstance As Long
  57.     UniqueID As Long
  58.     NameOffset As Long
  59.     NameLength As Long
  60. End Type
  61.  
  62. Private Type PERF_COUNTER_BLOCK
  63.     ByteLength As Long
  64. End Type
  65.  
  66. Private Type PERF_DATA_BLOCK
  67.     Signature As String * 4
  68.     LittleEndian As Long
  69.     Version As Long
  70.     Revision As Long
  71.     TotalByteLength As Long
  72.     HeaderLength As Long
  73.     NumObjectTypes As Long
  74.     DefaultObject As Long
  75.     SystemTime As SystemTime
  76.     PerfTime As LARGE_INTEGER
  77.     PerfFreq As LARGE_INTEGER
  78.     PerTime100nSec As LARGE_INTEGER
  79.     SystemNameLength As Long
  80.     SystemNameOffset As Long
  81. End Type
  82.  
  83. Private Type PERF_OBJECT_TYPE
  84.     TotalByteLength As Long
  85.     DefinitionLength As Long
  86.     HeaderLength As Long
  87.     ObjectNameTitleIndex As Long
  88.     ObjectNameTitle As Long
  89.     ObjectHelpTitleIndex As Long
  90.     ObjectHelpTitle As Long
  91.     DetailLevel As Long
  92.     NumCounters As Long
  93.     DefaultCounter As Long
  94.     NumInstances As Long
  95.     CodePage As Long
  96.     PerfTime As LARGE_INTEGER
  97.     PerfFreq As LARGE_INTEGER
  98. End Type
  99.  
  100. Private Type PERF_COUNTER_DEFINITION
  101.     ByteLength As Long
  102.     CounterNameTitleIndex As Long
  103.     CounterNameTitle As Long
  104.     CounterHelpTitleIndex As Long
  105.     CounterHelpTitle As Long
  106.     DefaultScale As Long
  107.     DetailLevel As Long
  108.     CounterType As Long
  109.     CounterSize As Long
  110.     CounterOffset As Long
  111. End Type
  112. '#define DEFAULT_BUFFER_SIZE 40960L
  113. Private Const DEFAULT_BUFFER_SIZE = 4096
  114. '//*************************************************************
  115. Private mvar_RemoteServer As String
  116. Private m_bLocalServer As Boolean
  117. Public Property Let RemoteServer(ByVal NewData As String)
  118.     If Len(NewData) Then
  119.         If Left(NewData, 2) = "\\" Then
  120.             mvar_RemoteServer = NewData
  121.         Else
  122.             mvar_RemoteServer = "\\" & NewData
  123.         End If
  124.         m_bLocalServer = True
  125.     Else
  126.         m_bLocalServer = False
  127.     End If
  128. End Property
  129. '//*************************************************************
  130.  
  131.  
  132. Public Function GetPerfMonValue(sInstanceValue As String, sDataValue As String)
  133. Dim lKeyRegistry As Long, lResReg As Long, ret As Long
  134. Dim lBufferSize As Long
  135. Dim lAllocSz As Long
  136. Dim perfDataBlock As PERF_DATA_BLOCK
  137. Dim perfObjectType As PERF_OBJECT_TYPE
  138. Dim perfCounterDefinition As PERF_COUNTER_DEFINITION
  139. Dim perfInstanceDefinition As PERF_INSTANCE_DEFINITION
  140. Dim perfCounterBlock As PERF_COUNTER_BLOCK
  141. Dim lDest As Long, lSrc As Long
  142. Dim i As Long
  143. Dim ptrPOT As Long, ptrPCB As Long, lCPU As Long
  144. Dim sInstanceName As String
  145. Dim ValCounter As Long
  146.  
  147.  
  148.     If m_bLocalServer Then
  149.         lResReg = RegConnectRegistry(mvar_RemoteServer, HKEY_PERFORMANCE_DATA, lKeyRegistry)
  150.         If lResReg <> 0 Then
  151.             GetPerfMonValue = -1
  152.         Exit Function
  153.         End If
  154.     Else
  155.         lKeyRegistry = HKEY_PERFORMANCE_DATA
  156.     End If
  157.         
  158.     lBufferSize = DEFAULT_BUFFER_SIZE
  159.     lAllocSz = DEFAULT_BUFFER_SIZE
  160.     
  161.     
  162.     ReDim aBuf(1 To lAllocSz) As Byte
  163.     Do
  164.         ret = RegQueryValueEx(lKeyRegistry, sInstanceValue, 0, 0, aBuf(1), lBufferSize)
  165.         If ret = ERROR_MORE_DATA Then
  166.         lAllocSz = lAllocSz + DEFAULT_BUFFER_SIZE
  167.         ReDim aBuf(1 To lAllocSz) As Byte
  168.         lBufferSize = lAllocSz
  169.         ElseIf ret = 0 Then
  170.             Exit Do
  171.         Else
  172.             Exit Function
  173.         End If
  174.     Loop While 1
  175.     
  176.     
  177.     
  178.     lDest = VarPtr(perfDataBlock)
  179.     lSrc = VarPtr(aBuf(1))
  180.     Memcopy ByVal lDest, ByVal lSrc, LenB(perfDataBlock)
  181.     lBufferSize = lAllocSz
  182.     If perfDataBlock.Signature <> "PERF" Then
  183.         Debug.Print "No valid PerfData"
  184.         
  185.     End If
  186.     lDest = VarPtr(perfObjectType)
  187.     lSrc = VarPtr(aBuf(1)) + perfDataBlock.HeaderLength
  188.     For i = 1 To perfDataBlock.NumObjectTypes
  189.         Memcopy ByVal lDest, ByVal lSrc, LenB(perfObjectType)
  190.         ptrPOT = lSrc
  191.         If perfObjectType.ObjectNameTitleIndex = sInstanceValue Then Exit For
  192.         lSrc = lSrc + perfObjectType.TotalByteLength
  193.     Next i
  194.     
  195.     If perfObjectType.ObjectNameTitleIndex <> sInstanceValue Then
  196.         'Err.Raise Err_CantFindProcessorPerfMon, ClassName & ".CollectData", "Unable to locate the 'Processor' performance object"
  197.         Debug.Print "No Instance found"
  198.     End If
  199.         
  200.     lDest = VarPtr(perfCounterDefinition)
  201.     lSrc = lSrc + perfObjectType.HeaderLength
  202.     For i = 1 To perfObjectType.NumCounters
  203.         Memcopy ByVal lDest, ByVal lSrc, LenB(perfCounterDefinition)
  204.         If perfCounterDefinition.CounterNameTitleIndex = sDataValue Then Exit For
  205.         lSrc = lSrc + perfCounterDefinition.ByteLength
  206.     Next i
  207.     If perfCounterDefinition.CounterNameTitleIndex <> sDataValue Then
  208.         Debug.Print "Have no Counter"
  209.     End If
  210.     lSrc = ptrPOT + perfObjectType.DefinitionLength
  211.     If perfObjectType.NumInstances > 0 Then
  212.         For i = 1 To perfObjectType.NumInstances
  213.             lDest = VarPtr(perfInstanceDefinition)
  214.             Memcopy ByVal lDest, ByVal lSrc, LenB(perfInstanceDefinition)
  215.             sInstanceName = Space(perfInstanceDefinition.NameLength - 2)
  216.             Memcopy ByVal sInstanceName, ByVal lSrc + perfInstanceDefinition.NameOffset, perfInstanceDefinition.NameLength - 2
  217.             
  218.             sInstanceName = StrConv(sInstanceName, vbFromUnicode)
  219.     
  220.             lSrc = lSrc + perfInstanceDefinition.ByteLength
  221.             lDest = VarPtr(perfCounterBlock)
  222.             Memcopy ByVal lDest, ByVal lSrc, LenB(perfCounterBlock)
  223.             ptrPCB = lSrc
  224.             If IsNumeric(sInstanceName) Then
  225.                 lCPU = CLng(sInstanceName)
  226.                 'm_aPrevCountersCPU(lCPU) = m_aCountersCPU(lCPU)
  227.                 Memcopy ByVal VarPtr(ValCounter), ByVal ptrPCB + perfCounterDefinition.CounterOffset, LenB(ValCounter)
  228.             End If
  229.     
  230.             lSrc = lSrc + perfCounterBlock.ByteLength
  231.         Next i
  232.     Else
  233.         '//So what now ?
  234.             lDest = VarPtr(perfInstanceDefinition)
  235.             ptrPCB = lSrc
  236.             Memcopy ByVal VarPtr(ValCounter), ByVal ptrPCB + perfCounterDefinition.CounterOffset, LenB(ValCounter)
  237.     End If
  238.     RegCloseKey lKeyRegistry    '//Important to close the key after processing
  239.     GetPerfMonValue = ValCounter
  240. End Function
  241.  
  242. Public Function GetRamAmount() As Currency
  243. Dim res As Long, lResultkey As Long
  244. Dim lReserve As Long, sClass As String, lClass As Long, ft As FILETIME, lName As Long, sName As String
  245. Dim nCnt As Long, lOpenResult
  246. Dim sKey As String, sTemp As String
  247. Dim xByte() As Byte
  248. Dim isX64 As Boolean, xStep As Integer
  249. Dim xSum As Double
  250. 'HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
  251.     If m_bLocalServer Then
  252.         res = RegConnectRegistry(mvar_RemoteServer, HKEY_LOCAL_MACHINE, lResultkey)
  253.         If res <> 0 Then
  254.             GetRamAmount = -1
  255.             Exit Function
  256.         End If
  257.     Else
  258.         lResultkey = HKEY_LOCAL_MACHINE
  259.     End If
  260.     
  261.     sKey = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
  262.     sTemp = GetRegistryValueB(lResultkey, sKey, "PROCESSOR_ARCHITECTURE")
  263.     
  264.     isX64 = (sTemp = "AMD64")   '//Both intel an AMD set this Value for 64 bit architecture
  265.     
  266.  
  267.     
  268.     sKey = "HARDWARE\RESOURCEMAP\System Resources\Physical Memory" & vbNullString
  269.     
  270.     xByte = GetRegistryValueB(lResultkey, sKey, ".Translated")
  271.     xStep = IIf(isX64, 20, 16)
  272.     xSum = 0
  273.     For nCnt = 32 To UBound(xByte) Step xStep
  274.         If isX64 Then
  275.             xSum = xSum + xByte(nCnt + 6) * 281474976710656#
  276.             xSum = xSum + xByte(nCnt + 5) * 1099511627776#
  277.             xSum = xSum + xByte(nCnt + 4) * 4294967296#
  278.             xSum = xSum + xByte(nCnt + 3) * 16777216#
  279.             xSum = xSum + xByte(nCnt + 2) * 65536#
  280.             xSum = xSum + xByte(nCnt + 1) * 256#
  281.             xSum = xSum + xByte(nCnt)
  282.         Else
  283.             xSum = xSum + xByte(nCnt + 3) * 16777216#
  284.             xSum = xSum + xByte(nCnt + 2) * 65536#
  285.             xSum = xSum + xByte(nCnt + 1) * 256#
  286.             xSum = xSum + xByte(nCnt)
  287.         End If
  288.  
  289.     Next
  290.     RegCloseKey lResultkey
  291.     GetRamAmount = xSum
  292.     
  293. End Function
  294.  
  295. Private Function GetRegistryValueB(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Variant
  296. Dim lResult As Long
  297. Dim lValueType As Long
  298. Dim lBuf As Long
  299. Dim lDataBufSize As Long
  300. Dim r As Long
  301. Dim Keyhand As Long
  302. Dim xBuf() As Byte
  303. Dim sResult As String
  304. Dim lValue As Long
  305.  
  306. r = RegOpenKey(hKey, strPath, Keyhand)
  307.  
  308.  ' Get length/data type
  309. lDataBufSize = 0
  310. '//Default = DWORD
  311. lResult = RegQueryValueEx(Keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
  312. If lResult = 234 Then
  313.     ReDim xBuf(lDataBufSize)
  314.     lResult = RegQueryValueEx(Keyhand, strValueName, 0&, lValueType, xBuf(0), lDataBufSize)
  315. Else
  316.     ReDim xBuf(lDataBufSize)
  317.     lResult = RegQueryValueEx(Keyhand, strValueName, 0&, lValueType, xBuf(0), lDataBufSize)
  318.     
  319. End If
  320.  
  321. If lResult = ERROR_SUCCESS Then
  322.     Select Case lValueType
  323.         Case 1 '//String
  324.             sResult = Left(StrConv(xBuf, vbUnicode), lDataBufSize - 1)
  325.             GetRegistryValueB = sResult
  326.         Case 8  '//Ressource
  327.             GetRegistryValueB = xBuf
  328.  
  329.     End Select
  330. End If
  331.  
  332. r = RegCloseKey(Keyhand)
  333.     
  334. End Function
  335.  
  336.