'CPU使用率
Declare Function NtQuerySystemInformation Lib "Ntdll.dll" Alias "NtQuerySystemInformation"(ByVal dwInfoType as Integer,ByVal lpStructure as Integer, ByVal dwSize as ULong, ByVal dwReserved as ULong) as Integer


Function CPU_UseRate() as Integer  '
   Dim spSysPerforfInfo(76) as LongInt ' SYSTEM_PERFORMANCE_INFORMATION
   Dim stSysTimeInfo(3) as LongInt ' SYSTEM_TIME_INFORMATION
   Dim curIdle as LongInt
   Dim curSystem as LongInt
   Dim fanhui as Integer
   Static lidOldIdle as LongInt '记录上次的空闲时间
   Static liOldSystem as LongInt '记录上次的系统时间
   Static sbSysBasicInfo as Integer  ' SYSTEM_BASIC_INFORMATION '储存基本信息
   'Const SYSTEM_BASICINFORMATION = 0&          '使用SYSTEM_BASIC_INFORMATION结构
   'Const SYSTEM_PERFORMANCEINFORMATION = 2&    '使用SYSTEM_PERFORMANCE_INFORMATION结构
   'Const SYSTEM_TIMEINFORMATION = 3&           '使用SYSTEM_TIME_INFORMATION结构
   fanhui = -1
   'If sbSysBasicInfo.bKeNumberProcessors = 0 Then
   'If NtQuerySystemInformation(0, @sbSysBasicInfo(0), SizeOf (SYSTEM_BASIC_INFORMATION), 0&) _
   '<> 0 Then Exit Function '获得CPU基本信息
   'End If
   'If NtQuerySystemInformation(3, @stSysTimeInfo(0), SizeOf(SYSTEM_TIME_INFORMATION), 0&) _
   '<> 0 Then  Exit Function '获得新的系统时间
   'If NtQuerySystemInformation(2, @spSysPerforfInfo(0), Len(spSysPerforfInfo), 0&) _
   '<> 0 Then Exit Function '获得新的CPU的空闲时间
   
   If sbSysBasicInfo = 0 Then
      Dim s as SYSTEM_INFO
      GetSystemInfo @s
      'll=SizeOf(UInteger)*5+24
      'tt=CAllocate(ll)
      'If NtQuerySystemInformation(0, tt,  ll, 0&) <> 0 Then Exit Function '获得CPU基本信息
      'sbSysBasicInfo=Peek(UByte , tt+ll-SizeOf(UInteger)) '获取CPU 有几核
      'Deallocate tt
      sbSysBasicInfo = s.dwNumberOfProcessors  '获取CPU 有几核
   End If
   
   If NtQuerySystemInformation(3, Cast(UInteger, @stSysTimeInfo(0)), 32, 0& ) <> 0 Then Exit Function '获得新的系统时间
   
   If NtQuerySystemInformation(2, Cast(UInteger, @spSysPerforfInfo(0)), 616, 0& ) <> 0 Then Exit Function '获得新的CPU的空闲时间
   
   '当前值 = 最新值 - 旧值
   'curIdle = spSysPerforfInfo.liIdleTime - lidOldIdle
   'curSystem = stSysTimeInfo.liKeSystemTime - liOldSystem
   curIdle = spSysPerforfInfo(0) - lidOldIdle
   curSystem = stSysTimeInfo(1) - liOldSystem
   
   If curSystem <> 0 Then curIdle = (curIdle / curSystem) * 100 '计算空闲率
   'Print curSystem,curIdle
   'fanhui = CLng(100 - curIdle / sbSysBasicInfo.bKeNumberProcessors) '计算利用率
   fanhui = CLng(100 - curIdle / sbSysBasicInfo) '计算利用率
   If fanhui < 0 Or lidOldIdle = 0 Then fanhui = 0
   'lidOldIdle = spSysPerforfInfo.liIdleTime
   'liOldSystem = stSysTimeInfo.liKeSystemTime
   lidOldIdle = spSysPerforfInfo(0)
   liOldSystem = stSysTimeInfo(1)
   
   Function = fanhui
End Function


'Type SYSTEM_PERFORMANCE_INFORMATION
'    liIdleTime as LongInt              '空闲时间
'    dwSpare(0 To 75) as LongInt
'End Type

'Type SYSTEM_BASIC_INFORMATION
'    dwUnknown1 as ULong                   '一直为0
'    uKeMaximumIncrement as ULong         '一个时钟的计量单位
'    uPageSize as ULong                   '一个内存页的大小
'    uMmNumberOfPhysicalPages as ULong    '系统管理着多少个页
'    uMmLowestPhysicalPage as ULong       '低端内存页
'    uMmHighestPhysicalPage as ULong      '高端内存页
'    uAllocationGranularity as UInteger
'    pLowestUserAddress as UInteger          '低端用户地址
'    pMmHighestUserAddress as UInteger       '高端用户地址
'    uKeActiveProcessors as UInteger         '激活的处理器
'    bKeNumberProcessors as UByte         '有多少个处理器
''    bUnknown2 as Byte
''    wUnknown3 as Short
'End Type

'Type SYSTEM_TIME_INFORMATION
'    liKeBootTime as LongInt             '系统被启动的时间
'    liKeSystemTime as LongInt 
'    liExpTimeZoneBias  as  LongInt
'    uCurrentTimeZoneId as ULong
'    dwReserved as DWord 
'End Type
