如何得到CPU使用效率和内存占用率?(200分)

  • 主题发起人 主题发起人 mahoo
  • 开始时间 开始时间
M

mahoo

Unregistered / Unconfirmed
GUEST, unregistred user!
在Windows NT 环境下,如何获取当前的CPU使用率和内存占用率?
 
得到总内存和可利用内存<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; memory:TMemoryStatus;<br>begin<br>&nbsp; memory.dwLength:=sizeof(memory);<br>&nbsp; GlobalMemoryStatus(memory);<br>&nbsp; ShowMessage('Total RAM / total memory: '+inttostr(memory.dwTotalPhys)+' Bytes');<br>&nbsp; ShowMessage('Freie RAM / available memory: '+inttostr(memory.dwavailPhys)+' Bytes');<br>end;<br><br> 用GetSystemInfo函数获取CPU信息 <br>  SysInfo: TSYSTEMINFO; <br>  Lbl_CPUName:Tlabel; <br>  GetSystemInfo(SysInfo);//获得CPU信息 <br>  case SysInfo.dwProcessorType of <br>  PROCESSOR_INTEL_386:Lbl_CPUName.Caption:=format('%d%s',[SysInfo.dwNumber Of Processors,'Intel80386']); <br>  PROCESSOR_INTEL_486:Lbl_CPUName.Caption:=format('%d%s',[SysInfo.dwNumber Of Processors, 'Intel 80486']); <br>  PROCESSOR_INTEL_PENTIUM:Lbl_CPUName.Caption:=format('%d%s',[SysInfo.dwNum <br>berOfProcessors, 'Intel Pentium']); <br>  PROCESSOR_MIPS_R4000:Lbl_CPUName.Caption:=format('%d%s',[SysInfo.dwNumberOfProcessors, 'MIPS R4000']); <br>  PROCESSOR_ALPHA_21064:Lbl_CPUName.Caption:=format('%d%s',[SysInfo.dwNumberOfProcessors, 'ALPHA 21064']); <br>  end;//把CPU信息显示在Lbl_CPUName中。<br><br><br>
 
unit adCpuUsage;<br><br>{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *<br>CPU Usage Measurement routines for Delphi and C++ Builder<br><br>Author: &nbsp; &nbsp; &nbsp; Alexey A. Dynnikov<br>EMail: &nbsp; &nbsp; &nbsp; &nbsp;aldyn@chat.ru<br>WebSite: &nbsp; &nbsp; &nbsp;http://www.aldyn.ru/<br>Support: &nbsp; &nbsp; &nbsp;Use the e-mail aldyn@chat.ru<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or support@aldyn.ru<br><br>Creation: &nbsp; &nbsp; Jul 8, 2000<br>Version: &nbsp; &nbsp; &nbsp;1.02<br><br>Legal issues: Copyright (C) 2000 by Alexey A. Dynnikov &lt;aldyn@chat.ru&gt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; This software is provided 'as-is', without any express or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; implied warranty. &nbsp;In no event will the author be held liable<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for any &nbsp;damages arising from the use of this software.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Permission is granted to anyone to use this software for any<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; purpose, including commercial applications, and to alter it<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and redistribute it freely, subject to the following<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; restrictions:<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1. The origin of this software must not be misrepresented,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;you must not claim that you wrote the original software.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If you use this software in a product, an acknowledgment<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;in the product documentation would be appreciated but is<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;not required.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2. Altered source versions must be plainly marked as such, and<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;must not be misrepresented as being the original software.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3. This notice may not be removed or altered from any source<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;distribution.<br><br>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br>USAGE:<br><br>1. Include this unit into project.<br><br>2. Call GetCPUCount to obtain the numbr of processors in the system<br><br>3. Each time you need to know the value of CPU usage call the CollectCPUData<br>&nbsp; &nbsp;to refresh the CPU usage information. Then call the GetCPUUsage to obtain<br>&nbsp; &nbsp;the CPU usage for given processor. Note that succesive calls of GetCPUUsage<br>&nbsp; &nbsp;without calling CollectCPUData will return the same CPU usage value.<br><br>Example:<br><br>procedure TTestForm.TimerTimer(Sender: TObject);<br>var i: Integer;<br>begin<br>&nbsp; &nbsp; CollectCPUData; // Get the data for all processors<br><br>&nbsp; &nbsp; for i:=0 to GetCPUCount-1 do // Show data for each processor<br>&nbsp; &nbsp; &nbsp; &nbsp; MInfo.Lines:=Format('CPU #%d - %5.2f%%',[i,GetCPUUsage(i)*100]);<br>end;<br>* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}<br><br>interface<br><br>uses<br>&nbsp; &nbsp; Windows, SysUtils;<br><br>// Call CollectCPUData to refresh information about CPU usage<br>procedure CollectCPUData;<br><br>// Call it to obtain the number of CPU's in the system<br>function GetCPUCount: Integer;<br><br>// Call it to obtain the % of usage for given CPU<br>function GetCPUUsage(Index: Integer): Double;<br><br>// For Win9x only: call it to stop CPU usage monitoring and free system resources<br>procedure ReleaseCPUData;<br><br>implementation<br><br>{$ifndef ver110}<br><br>&nbsp; &nbsp; {$ifndef ver90}<br>&nbsp; &nbsp; {$ifndef ver100}<br>&nbsp; &nbsp; {$define UseInt64}<br>&nbsp; &nbsp; {$endif}<br>&nbsp; &nbsp; {$endif}<br><br><br>&nbsp; &nbsp; {$ifdef UseInt64}<br>&nbsp; &nbsp; type TInt64 = Int64;<br>&nbsp; &nbsp; {$else}<br>&nbsp; &nbsp; type TInt64 = Comp;<br>&nbsp; &nbsp; {$endif}<br><br>{$else}<br><br>&nbsp; &nbsp; type TInt64 = TLargeInteger;<br><br>{$endif}<br><br>type<br>&nbsp; &nbsp; PInt64 = ^TInt64;<br><br>type<br>&nbsp; &nbsp; TPERF_DATA_BLOCK = record<br>&nbsp; &nbsp; &nbsp; &nbsp; Signature : array[0..4 - 1] of WCHAR;<br>&nbsp; &nbsp; &nbsp; &nbsp; LittleEndian : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; Version : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; Revision : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; TotalByteLength : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; HeaderLength : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; NumObjectTypes : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; DefaultObject : Longint;<br>&nbsp; &nbsp; &nbsp; &nbsp; SystemTime : TSystemTime;<br>&nbsp; &nbsp; &nbsp; &nbsp; Reserved: DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; PerfTime : TInt64;<br>&nbsp; &nbsp; &nbsp; &nbsp; PerfFreq : TInt64;<br>&nbsp; &nbsp; &nbsp; &nbsp; PerfTime100nSec : TInt64;<br>&nbsp; &nbsp; &nbsp; &nbsp; SystemNameLength : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; SystemNameOffset : DWORD;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; PPERF_DATA_BLOCK = ^TPERF_DATA_BLOCK;<br><br>&nbsp; &nbsp; TPERF_OBJECT_TYPE = record<br>&nbsp; &nbsp; &nbsp; &nbsp; TotalByteLength : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; DefinitionLength : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; HeaderLength : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; ObjectNameTitleIndex : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; ObjectNameTitle : LPWSTR;<br>&nbsp; &nbsp; &nbsp; &nbsp; ObjectHelpTitleIndex : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; ObjectHelpTitle : LPWSTR;<br>&nbsp; &nbsp; &nbsp; &nbsp; DetailLevel : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; NumCounters : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; DefaultCounter : Longint;<br>&nbsp; &nbsp; &nbsp; &nbsp; NumInstances : Longint;<br>&nbsp; &nbsp; &nbsp; &nbsp; CodePage : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; PerfTime : TInt64;<br>&nbsp; &nbsp; &nbsp; &nbsp; PerfFreq : TInt64;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; PPERF_OBJECT_TYPE = ^TPERF_OBJECT_TYPE;<br><br>type<br>&nbsp; &nbsp; TPERF_COUNTER_DEFINITION = record<br>&nbsp; &nbsp; &nbsp; &nbsp; ByteLength : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; CounterNameTitleIndex : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; CounterNameTitle : LPWSTR;<br>&nbsp; &nbsp; &nbsp; &nbsp; CounterHelpTitleIndex : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; CounterHelpTitle : LPWSTR;<br>&nbsp; &nbsp; &nbsp; &nbsp; DefaultScale : Longint;<br>&nbsp; &nbsp; &nbsp; &nbsp; DetailLevel : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; CounterType : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; CounterSize : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; CounterOffset : DWORD;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; PPERF_COUNTER_DEFINITION = ^TPERF_COUNTER_DEFINITION;<br><br>&nbsp; &nbsp; TPERF_COUNTER_BLOCK = record<br>&nbsp; &nbsp; &nbsp; &nbsp; ByteLength : DWORD;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; PPERF_COUNTER_BLOCK = ^TPERF_COUNTER_BLOCK;<br><br>&nbsp; &nbsp; TPERF_INSTANCE_DEFINITION = record<br>&nbsp; &nbsp; &nbsp; &nbsp; ByteLength : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; ParentObjectTitleIndex : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; ParentObjectInstance : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; UniqueID : Longint;<br>&nbsp; &nbsp; &nbsp; &nbsp; NameOffset : DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; NameLength : DWORD;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; PPERF_INSTANCE_DEFINITION = ^TPERF_INSTANCE_DEFINITION;<br><br>//------------------------------------------------------------------------------<br>{$ifdef ver130}<br>{$L-} &nbsp; &nbsp; &nbsp; &nbsp; // The L+ causes internal error in Delphi 5 compiler<br>{$O-} &nbsp; &nbsp; &nbsp; &nbsp; // The O+ causes internal error in Delphi 5 compiler<br>{$Y-} &nbsp; &nbsp; &nbsp; &nbsp; // The Y+ causes internal error in Delphi 5 compiler<br>{$endif}<br><br>{$ifndef ver110}<br>type<br>&nbsp; &nbsp; TInt64F = TInt64;<br>{$else}<br>type<br>&nbsp; &nbsp; TInt64F = Extended;<br>{$endif}<br><br>{$ifdef ver110}<br>function FInt64(Value: TInt64): TInt64F;<br>function Int64D(Value: DWORD): TInt64;<br>{$else}<br>type<br>&nbsp; &nbsp; FInt64 = TInt64F;<br>&nbsp; &nbsp; Int64D = TInt64;<br>{$endif}<br><br>{$ifdef ver110}<br>function FInt64(Value: TInt64): TInt64F;<br>var V: TInt64;<br>begin<br>&nbsp; &nbsp; if (Value.HighPart and $80000000) = 0 then // positive value<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=Value.HighPart;<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=result*$10000*$10000;<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=result+Value.LowPart;<br>&nbsp; &nbsp; end else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; V.HighPart:=Value.HighPart xor $FFFFFFFF;<br>&nbsp; &nbsp; &nbsp; &nbsp; V.LowPart:=Value.LowPart xor $FFFFFFFF;<br>&nbsp; &nbsp; &nbsp; &nbsp; result:= -1 - FInt64(V);<br>&nbsp; &nbsp; end;<br>end;<br><br>function Int64D(Value: DWORD): TInt64;<br>begin<br>&nbsp; &nbsp; result.LowPart:=Value;<br>&nbsp; &nbsp; result.HighPart := 0; // positive only<br>end;<br>{$endif}<br><br>//------------------------------------------------------------------------------<br><br>const<br>&nbsp; &nbsp; Processor_IDX_Str = '238';<br>&nbsp; &nbsp; Processor_IDX = 238;<br>&nbsp; &nbsp; CPUUsageIDX = 6;<br><br>type<br>&nbsp; &nbsp; AInt64F = array[0..$FFFF] of TInt64F;<br>&nbsp; &nbsp; PAInt64F = ^AInt64F;<br><br>var<br>&nbsp; &nbsp; _PerfData : PPERF_DATA_BLOCK;<br>&nbsp; &nbsp; _BufferSize: Integer;<br>&nbsp; &nbsp; _POT : PPERF_OBJECT_TYPE;<br>&nbsp; &nbsp; _PCD: PPerf_Counter_Definition;<br>&nbsp; &nbsp; _ProcessorsCount: Integer;<br>&nbsp; &nbsp; _Counters: PAInt64F;<br>&nbsp; &nbsp; _PrevCounters: PAInt64F;<br>&nbsp; &nbsp; _SysTime: TInt64F;<br>&nbsp; &nbsp; _PrevSysTime: TInt64F;<br>&nbsp; &nbsp; _IsWinNT: Boolean;<br><br>&nbsp; &nbsp; _W9xCollecting: Boolean;<br>&nbsp; &nbsp; _W9xCpuUsage: DWORD;<br>&nbsp; &nbsp; _W9xCpuKey: HKEY;<br><br><br>//------------------------------------------------------------------------------<br>function GetCPUCount: Integer;<br>begin<br>&nbsp; &nbsp; if _IsWinNT then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if _ProcessorsCount &lt; 0 then CollectCPUData;<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=_ProcessorsCount;<br>&nbsp; &nbsp; end else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=1;<br>&nbsp; &nbsp; end;<br><br>end;<br><br>//------------------------------------------------------------------------------<br>procedure ReleaseCPUData;<br>var H: HKEY;<br>&nbsp; &nbsp; R: DWORD;<br>&nbsp; &nbsp; dwDataSize, dwType: DWORD;<br>begin<br>&nbsp; &nbsp; if _IsWinNT then exit;<br>&nbsp; &nbsp; if not _W9xCollecting then exit;<br>&nbsp; &nbsp; _W9xCollecting:=False;<br><br>&nbsp; &nbsp; RegCloseKey(_W9xCpuKey);<br><br>&nbsp; &nbsp; R:=RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StopStat', 0, KEY_ALL_ACCESS, H );<br><br>&nbsp; &nbsp; if R &lt;&gt; ERROR_SUCCESS then exit;<br><br>&nbsp; &nbsp; dwDataSize:=sizeof(DWORD);<br><br>&nbsp; &nbsp; RegQueryValueEx ( H, 'KERNEL/CPUUsage', nil, @dwType, PBYTE(@_W9xCpuUsage), @dwDataSize);<br><br>&nbsp; &nbsp; RegCloseKey(H);<br><br>end;<br><br>//------------------------------------------------------------------------------<br>function GetCPUUsage(Index: Integer): Double;<br>begin<br>&nbsp; &nbsp; if _IsWinNT then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if _ProcessorsCount &lt; 0 then CollectCPUData;<br>&nbsp; &nbsp; &nbsp; &nbsp; if (Index &gt;= _ProcessorsCount) or (Index &lt; 0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('CPU index out of bounds');<br>&nbsp; &nbsp; &nbsp; &nbsp; if _PrevSysTime = _SysTime then result:=0 else<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=1-(_Counters[index] - _PrevCounters[index])/(_SysTime-_PrevSysTime);<br>&nbsp; &nbsp; end else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if Index &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('CPU index out of bounds');<br>&nbsp; &nbsp; &nbsp; &nbsp; if not _W9xCollecting then CollectCPUData;<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=_W9xCpuUsage / 100;<br>&nbsp; &nbsp; end;<br>end;<br><br>var VI: TOSVERSIONINFO;<br><br>//------------------------------------------------------------------------------<br>procedure CollectCPUData;<br>var BS: integer;<br>&nbsp; &nbsp; i: Integer;<br>&nbsp; &nbsp; _PCB_Instance: PPERF_COUNTER_BLOCK;<br>&nbsp; &nbsp; _PID_Instance: PPERF_INSTANCE_DEFINITION;<br>&nbsp; &nbsp; ST: TFileTime;<br><br>var H: HKEY;<br>&nbsp; &nbsp; R: DWORD;<br>&nbsp; &nbsp; dwDataSize, dwType: DWORD;<br>begin<br>&nbsp; &nbsp; if _IsWinNT then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; BS:=_BufferSize;<br>&nbsp; &nbsp; &nbsp; &nbsp; while RegQueryValueEx( HKEY_PERFORMANCE_DATA, Processor_IDX_Str, nil, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PByte(_PerfData), @BS ) = ERROR_MORE_DATA do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get a buffer that is big enough.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INC(_BufferSize,$1000);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BS:=_BufferSize;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReallocMem( _PerfData, _BufferSize );<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // Locate the performance object<br>&nbsp; &nbsp; &nbsp; &nbsp; _POT := PPERF_OBJECT_TYPE(DWORD(_PerfData) + _PerfData.HeaderLength);<br>&nbsp; &nbsp; &nbsp; &nbsp; for i := 1 to _PerfData.NumObjectTypes do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if _POT.ObjectNameTitleIndex = Processor_IDX then Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _POT := PPERF_OBJECT_TYPE(DWORD(_POT) + _POT.TotalByteLength);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // Check for success<br>&nbsp; &nbsp; &nbsp; &nbsp; if _POT.ObjectNameTitleIndex &lt;&gt; Processor_IDX then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('Unable to locate the "Processor" performance object');<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if _ProcessorsCount &lt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ProcessorsCount:=_POT.NumInstances;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetMem(_Counters,_ProcessorsCount*SizeOf(TInt64));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetMem(_PrevCounters,_ProcessorsCount*SizeOf(TInt64));<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // Locate the "% CPU usage" counter definition<br>&nbsp; &nbsp; &nbsp; &nbsp; _PCD := PPERF_Counter_DEFINITION(DWORD(_POT) + _POT.HeaderLength);<br>&nbsp; &nbsp; &nbsp; &nbsp; for i := 1 to _POT.NumCounters do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if _PCD.CounterNameTitleIndex = CPUUsageIDX then break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _PCD := PPERF_COUNTER_DEFINITION(DWORD(_PCD) + _PCD.ByteLength);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // Check for success<br>&nbsp; &nbsp; &nbsp; &nbsp; if _PCD.CounterNameTitleIndex &lt;&gt; CPUUsageIDX then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('Unable to locate the "% of CPU usage" performance counter');<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // Collecting coutners<br>&nbsp; &nbsp; &nbsp; &nbsp; _PID_Instance := PPERF_INSTANCE_DEFINITION(DWORD(_POT) + _POT.DefinitionLength);<br>&nbsp; &nbsp; &nbsp; &nbsp; for i := 0 to _ProcessorsCount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _PCB_Instance := PPERF_COUNTER_BLOCK(DWORD(_PID_Instance) + _PID_Instance.ByteLength );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _PrevCounters:=_Counters;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _Counters:=FInt64(PInt64(DWORD(_PCB_Instance) + _PCD.CounterOffset)^);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _PID_Instance := PPERF_INSTANCE_DEFINITION(DWORD(_PCB_Instance) + _PCB_Instance.ByteLength);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; _PrevSysTime:=_SysTime;<br>&nbsp; &nbsp; &nbsp; &nbsp; SystemTimeToFileTime(_PerfData.SystemTime, ST);<br>&nbsp; &nbsp; &nbsp; &nbsp; _SysTime:=FInt64(TInt64(ST));<br>&nbsp; &nbsp; end else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if not _W9xCollecting then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R:=RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StartStat', 0, KEY_ALL_ACCESS, H );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if R &lt;&gt; ERROR_SUCCESS then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('Unable to start performance monitoring');<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwDataSize:=sizeof(DWORD);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RegQueryValueEx( H, 'KERNEL/CPUUsage', nil, @dwType, PBYTE(@_W9xCpuUsage), @dwDataSize );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RegCloseKey(H);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R:=RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StatData', 0,KEY_READ, _W9xCpuKey );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if R &lt;&gt; ERROR_SUCCESS then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('Unable to read performance data');<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _W9xCollecting:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; dwDataSize:=sizeof(DWORD);<br>&nbsp; &nbsp; &nbsp; &nbsp; RegQueryValueEx( _W9xCpuKey, 'KERNEL/CPUUsage', nil,@dwType, PBYTE(@_W9xCpuUsage), @dwDataSize );<br>&nbsp; &nbsp; end;<br>end;<br><br><br>initialization<br>&nbsp; &nbsp; _ProcessorsCount:= -1;<br>&nbsp; &nbsp; _BufferSize:= $2000;<br>&nbsp; &nbsp; _PerfData := AllocMem(_BufferSize);<br><br>&nbsp; &nbsp; VI.dwOSVersionInfoSize:=SizeOf(VI);<br>&nbsp; &nbsp; if not GetVersionEx(VI) then raise Exception.Create('Can''t get the Windows version');<br><br>&nbsp; &nbsp; _IsWinNT := VI.dwPlatformId = VER_PLATFORM_WIN32_NT;<br>finalization<br>&nbsp; &nbsp; ReleaseCPUData;<br>&nbsp; &nbsp; FreeMem(_PerfData);<br>end.<br><br>
 
转一个<br>问题提出/摘要:<br>从注册表获取如活动进程数、CPU占用率等信息? <br>&nbsp;<br><br>回答:<br>&nbsp; 在注册表中,有一个分支叫HKEY_DYN_DATA,大量的系统信息都保存在此分支下的PerfStats主键中。<br>&nbsp; <br>&nbsp; 为了获取如CPU的使用率等信息,你需要做三件事:<br>&nbsp; <br>&nbsp; 第一步是开始选择的计数器,可以通过读取相应的注册表主键完成。<br>&nbsp; <br>&nbsp; 例如:<br>&nbsp; 如果我们想知道CPU的占用率,你可以读HKEY_DYN_DATA的这个主键<br>&nbsp; 'PerfStats/StartStat/KERNEL/CPUusage' <br>&nbsp; 这一步是开始计数器<br>&nbsp; <br>&nbsp; 下一步是是读取'PerfStats/StatData/KERNEL/CPUusage'的值,它的值给出了当前CPU的使用率。如果我们将代码放在一个Timer的事件中,我们就可以看到动态的变化了。<br>&nbsp; <br>&nbsp; 最后一步是停止计数器,通过读取注册表的'PerfStats/StopStat/KERNEL/CPUusage'键完成。<br>&nbsp; <br>&nbsp; 除CPU的占用率外,这里还有很多其它的计数器。你可以打开用注册表编辑器打开PerfStats/StatData键,可以看到全部的列表。<br>&nbsp; <br>&nbsp; 我生成了一个构件来获取注册表HKEY_DYN_DATA分支提供的所有计数的值。以下是代码:<br>&nbsp; <br>&nbsp; unit SystemInfo; <br>&nbsp; <br>&nbsp; interface <br>&nbsp; <br>&nbsp; uses <br>&nbsp; &nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, <br>&nbsp; &nbsp; Dialogs,extctrls; <br>&nbsp; <br>&nbsp; type TDialupAdapterInfo = record //Information of Dialup adapter <br>&nbsp; &nbsp; alignment:dword; <br>&nbsp; &nbsp; buffer:dword; <br>&nbsp; &nbsp; bytesrecieved:dword; <br>&nbsp; &nbsp; bytesXmit:dword; <br>&nbsp; &nbsp; ConnectSpeed:dword; <br>&nbsp; &nbsp; CRC:dword; <br>&nbsp; &nbsp; framesrecieved:dword; <br>&nbsp; &nbsp; FramesXmit:dword; <br>&nbsp; &nbsp; Framing:dword; <br>&nbsp; &nbsp; runts:dword; <br>&nbsp; &nbsp; Overrun:dword; <br>&nbsp; &nbsp; timeout:dword; <br>&nbsp; &nbsp; totalbytesrecieved:dword; <br>&nbsp; &nbsp; totalbytesXmit:dword; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; type TKernelInfo = record <br>&nbsp; &nbsp; CpuUsagePcnt:dword; <br>&nbsp; &nbsp; Numthreads:dword; <br>&nbsp; &nbsp; NumVMS:dword; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; type TVCACHEInfo = record <br>&nbsp; &nbsp; ccurpages:dword; <br>&nbsp; &nbsp; cMacPages:dword; <br>&nbsp; &nbsp; cminpages:dword; <br>&nbsp; &nbsp; FailedRecycles:dword; <br>&nbsp; &nbsp; Hits:dword; <br>&nbsp; &nbsp; LRUBuffers:dword; <br>&nbsp; &nbsp; LRURecycles:dword; <br>&nbsp; &nbsp; Misses:dword; <br>&nbsp; &nbsp; RandomRecycles:dword; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; type TFATInfo = record <br>&nbsp; &nbsp; BreadsSec:dword; <br>&nbsp; &nbsp; BwritesSec:dword; <br>&nbsp; &nbsp; Dirtydata:dword; <br>&nbsp; &nbsp; ReadsSec:dword; <br>&nbsp; &nbsp; WritesSec:dword; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; type TVMMInfo = record <br>&nbsp; &nbsp; CDiscards:dword; <br>&nbsp; &nbsp; CInstancefaults:dword; <br>&nbsp; &nbsp; CPageFaults:dword; <br>&nbsp; &nbsp; cPageIns:dword; <br>&nbsp; &nbsp; cPageOuts:dword; <br>&nbsp; &nbsp; cpgCommit:dword; <br>&nbsp; &nbsp; cpgDiskCache:dword; <br>&nbsp; &nbsp; cpgDiskCacheMac:dword; <br>&nbsp; &nbsp; cpgDiskCacheMid:dword; <br>&nbsp; &nbsp; cpgDiskCacheMin:dword; <br>&nbsp; &nbsp; cpgfree:dword; <br>&nbsp; <br>&nbsp; &nbsp; cpglocked:dword; <br>&nbsp; &nbsp; cpglockedNoncache:dword; <br>&nbsp; &nbsp; cpgother:dword; <br>&nbsp; &nbsp; cpgsharedpages:dword; <br>&nbsp; &nbsp; cpgswap:dword; <br>&nbsp; &nbsp; cpgswapfile:dword; <br>&nbsp; &nbsp; cpgswapfiledefective:dword; <br>&nbsp; &nbsp; cpgswapfileinuse:dword; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; type <br>&nbsp; &nbsp; TSysInfo = class(TComponent) <br>&nbsp; &nbsp; private <br>&nbsp; &nbsp; &nbsp;fDialupAdapterInfo:TDialupAdapterInfo; <br>&nbsp; &nbsp; &nbsp;fKernelInfo:TKernelInfo; <br>&nbsp; &nbsp; &nbsp;fVCACHEInfo:TVCACHEInfo; <br>&nbsp; &nbsp; &nbsp;fFATInfo:TFATInfo; <br>&nbsp; &nbsp; &nbsp;fVMMInfo:TVMMInfo; <br>&nbsp; &nbsp; &nbsp;ftimer:TTimer; <br>&nbsp; &nbsp; &nbsp;fupdateinterval:integer; <br>&nbsp; &nbsp; &nbsp;tmp:dword; <br>&nbsp; &nbsp; &nbsp;vsize:dword; <br>&nbsp; &nbsp; &nbsp;pkey:hkey; <br>&nbsp; &nbsp; &nbsp;regtype:pdword; <br>&nbsp; &nbsp; &nbsp;fstopped:boolean; <br>&nbsp; &nbsp; &nbsp;procedure fupdatinginfo(sender:tobject); <br>&nbsp; &nbsp; &nbsp;procedure fsetupdateinterval(aupdateinterval:integer); <br>&nbsp; &nbsp; protected <br>&nbsp; &nbsp; &nbsp;fsysInfoChanged:TNotifyEvent; <br>&nbsp; &nbsp; public <br>&nbsp; &nbsp; &nbsp;constructor Create(Aowner:Tcomponent);override; <br>&nbsp; &nbsp; &nbsp;destructor &nbsp;Destroy;override; <br>&nbsp; <br>&nbsp; &nbsp; &nbsp;property DialupAdapterInfo: TDialupAdapterInfo read fDialupAdapterInfo; <br>&nbsp; &nbsp; &nbsp;property KernelInfo: TKernelInfo read fKernelInfo; <br>&nbsp; &nbsp; &nbsp;property VCACHEInfo: TVCACHEInfo read fVCACHEInfo; <br>&nbsp; &nbsp; &nbsp;property FATInfo: TFATInfo read fFATInfo; <br>&nbsp; &nbsp; &nbsp;property VMMInfo: TVMMInfo read fVMMInfo; <br>&nbsp; &nbsp; &nbsp;procedure StartRecievingInfo; <br>&nbsp; &nbsp; &nbsp;procedure StopRecievingInfo; <br>&nbsp; &nbsp; published <br>&nbsp; &nbsp; &nbsp;property SysInfoChanged:TNotifyEvent read fsysInfoChanged write <br>&nbsp; &nbsp; &nbsp; fsysInfoChanged;//this event is called after a specific interval. <br>&nbsp; &nbsp; &nbsp;property UpdateInterval:integer read fupdateInterval write <br>&nbsp; &nbsp; &nbsp; fsetupdateinterval default 5000; <br>&nbsp; &nbsp; end; <br>&nbsp; <br>&nbsp; procedure Register; <br>&nbsp; <br>&nbsp; implementation <br>&nbsp; <br>&nbsp; constructor TSysInfo.Create(Aowner:Tcomponent); <br>&nbsp; begin <br>&nbsp; inherited; <br>&nbsp; ftimer:=ttimer.Create(self); <br>&nbsp; ftimer.enabled:=false; <br>&nbsp; ftimer.OnTimer:=fupdatinginfo; <br>&nbsp; vsize:=4; <br>&nbsp; fstopped:=true; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure TSysInfo.startrecievingInfo; <br>&nbsp; var <br>&nbsp; res:integer; <br>&nbsp; begin <br>&nbsp; res:=RegOpenKeyEx(HKEY_DYN_DATA,'PerfStats/StartStat',0,KEY_ALL_ACCESS,pkey); <br>&nbsp; if res&lt;&gt;0 then <br>&nbsp; &nbsp; raise exception.Create('Could not open registry key'); <br>&nbsp; fstopped:=false; <br>&nbsp; // For Dial Up Adapter <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Alignment',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Buffer',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Framing',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Overrun ',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Timeout',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/CRC',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Runts',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/FramesXmit',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/FramesRecvd',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/BytesRecvd',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesXmit',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesRecvd',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/ConnectSpeed',nil,regtype,@tmp,@vsize); <br>&nbsp; <br>&nbsp; // For VCACHE <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/LRUBuffers',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/FailedRecycles',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/RandomRecycles',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/LRURecycles',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/Misses',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/Hits',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/cMacPages',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/cMinPages',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/cCurPages',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@tmp,@vsize); <br>&nbsp; <br>&nbsp; //For VFAT <br>&nbsp; <br>&nbsp; RegQueryValueEx(pkey,'VFAT/DirtyData',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VFAT/BReadsSec',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VFAT/BWritesSec',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VFAT/ReadsSec',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VFAT/WritesSec',nil,regtype,@tmp,@vsize); <br>&nbsp; //For VMM <br>&nbsp; <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgLockedNoncache',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgCommit',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSharedPages',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgDiskcacheMid',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgDiskcacheMac',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgDiskcacheMin',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgDiskcache',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSwapfileDefective',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSwapfileInUse',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSwapfile',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cDiscards',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cPageOuts',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cPageIns',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cInstanceFaults',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cPageFaults',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgOther',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSwap',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgLocked',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgFree',nil,regtype,@tmp,@vsize); <br>&nbsp; //For KERNEL <br>&nbsp; RegQueryValueEx(pkey,'KERNEL/CPUUsage',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'KERNEL/VMs',nil,regtype,@tmp,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'KERNEL/Threads',nil,regtype,@tmp,@vsize); <br>&nbsp; RegCloseKey(pkey); <br>&nbsp; ftimer.enabled:=true; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure tsysinfo.fupdatinginfo(sender:tobject); <br>&nbsp; var <br>&nbsp; res:integer; <br>&nbsp; begin <br>&nbsp; res:=RegOpenKeyEx(HKEY_DYN_DATA,'PerfStats/StatData',0,KEY_ALL_ACCESS,pkey); <br>&nbsp; if res&lt;&gt;0 then <br>&nbsp; &nbsp; raise exception.Create('Could not open registry key'); <br>&nbsp; //For Dial Up Adapter <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Alignment',nil,regtype,@fDialupAdapterInfo.alignment,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Buffer',nil,regtype,@fDialupAdapterInfo.buffer,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Framing',nil,regtype,@fDialupAdapterInfo.framing,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Overrun ',nil,regtype,@fDialupAdapterInfo.overrun,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Timeout',nil,regtype,@fDialupAdapterInfo.timeout,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/CRC',nil,regtype,@fDialupAdapterInfo.crc,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/Runts',nil,regtype,@fDialupAdapterInfo.runts,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/FramesXmit',nil,regtype,@fDialupAdapterInfo.framesxmit,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/FramesRecvd',nil,regtype,@fDialupAdapterInfo.framesrecieved,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@fDialupAdapterInfo.bytesxmit,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/BytesRecvd',nil,regtype,@fDialupAdapterInfo.bytesrecieved,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesXmit',nil,regtype,@fDialupAdapterInfo.totalbytesxmit,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesRecvd',nil,regtype,@fDialupAdapterInfo.totalbytesrecieved,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'Dial-Up Adapter/ConnectSpeed',nil,regtype,@fDialupAdapterInfo.connectspeed,@vsize); <br>&nbsp; // For VCACHE <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/LRUBuffers',nil,regtype,@fVCACHEInfo.lrubuffers,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/FailedRecycles',nil,regtype,@fVCACHEInfo.failedrecycles,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/RandomRecycles',nil,regtype,@fVCACHEInfo.randomrecycles,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/LRURecycles',nil,regtype,@fVCACHEInfo.lrurecycles,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/Misses',nil,regtype,@fVCACHEInfo.misses,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/Hits',nil,regtype,@fVCACHEInfo.hits,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/cMacPages',nil,regtype,@fVCACHEInfo.cmacpages,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/cMinPages',nil,regtype,@fVCACHEInfo.cminpages,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VCACHE/cCurPages',nil,regtype,@fVCACHEInfo.ccurpages,@vsize); <br>&nbsp; //For VFAT <br>&nbsp; RegQueryValueEx(pkey,'VFAT/DirtyData',nil,regtype,@ffatinfo.dirtydata,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VFAT/BReadsSec',nil,regtype,@ffatinfo.breadssec,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VFAT/BWritesSec',nil,regtype,@ffatinfo.bwritessec,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VFAT/ReadsSec',nil,regtype,@ffatinfo.readssec,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VFAT/WritesSec',nil,regtype,@ffatinfo.writessec,@vsize); <br>&nbsp; //For VMM <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgLockedNoncache',nil,regtype,@fvmminfo.cpglockednoncache,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgCommit',nil,regtype,@fvmminfo.cpgcommit,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSharedPages',nil,regtype,@fvmminfo.cpgsharedpages,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgDiskcacheMid',nil,regtype,@fvmminfo.cpgdiskcacheMid,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgDiskcacheMac',nil,regtype,@fvmminfo.cpgdiskcacheMac,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgDiskcacheMin',nil,regtype,@fvmminfo.cpgdiskcacheMin,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgDiskcache',nil,regtype,@fvmminfo.cpgdiskcache,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSwapfileDefective',nil,regtype,@fvmminfo.cpgswapfiledefective,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSwapfileInUse',nil,regtype,@fvmminfo.cpgswapfileinuse,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSwapfile',nil,regtype,@fvmminfo.cpgswapfile,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cDiscards',nil,regtype,@fvmminfo.cdiscards,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cPageOuts',nil,regtype,@fvmminfo.cpageouts,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cPageIns',nil,regtype,@fvmminfo.cpageins,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cInstanceFaults',nil,regtype,@fvmminfo.cinstancefaults,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cPageFaults',nil,regtype,@fvmminfo.cpagefaults,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgOther',nil,regtype,@fvmminfo.cpgother,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgSwap',nil,regtype,@fvmminfo.cpgswap,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgLocked',nil,regtype,@fvmminfo.cpglocked,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'VMM/cpgFree',nil,regtype,@fvmminfo.cpgfree,@vsize); <br>&nbsp; //For KERNEL <br>&nbsp; RegQueryValueEx(pkey,'KERNEL/CPUUsage',nil,regtype,@fkernelinfo.cpuusagepcnt,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'KERNEL/VMs',nil,regtype,@fkernelinfo.numvms,@vsize); <br>&nbsp; RegQueryValueEx(pkey,'KERNEL/Threads',nil,regtype,@fkernelinfo.numThreads,@vsize); <br>&nbsp; RegCloseKey(pkey); <br>&nbsp; if assigned(SysInfoChanged) then <br>&nbsp; &nbsp; SysInfoChanged(self); <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure TSysInfo.stoprecievingInfo; <br>&nbsp; var <br>&nbsp; res:integer; <br>&nbsp; begin <br>&nbsp; res:=RegOpenKeyEx(HKEY_DYN_DATA,'PerfStats/StopStat',0,KEY_ALL_ACCESS,pkey); <br>&nbsp; if not fstopped then <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp;if res&lt;&gt;0 then <br>&nbsp; &nbsp; &nbsp; raise exception.Create('Could not open registry key'); <br>&nbsp; &nbsp; &nbsp;//For Dial Up Adapter <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/Alignment',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/Buffer',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/Framing',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/Overrun ',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/Timeout',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/CRC',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/Runts',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/FramesXmit',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/FramesRecvd',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/BytesRecvd',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesXmit',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/TotalBytesRecvd',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/ConnectSpeed',nil,regtype,@tmp,@vsize); <br>&nbsp; <br>&nbsp; &nbsp; &nbsp;// For VCACHE <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/LRUBuffers',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/FailedRecycles',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/RandomRecycles',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/LRURecycles',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/Misses',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/Hits',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/cMacPages',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/cMinPages',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VCACHE/cCurPages',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'Dial-Up Adapter/BytesXmit',nil,regtype,@tmp,@vsize); <br>&nbsp; <br>&nbsp; &nbsp; &nbsp;//For VFAT <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VFAT/DirtyData',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VFAT/BReadsSec',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VFAT/BWritesSec',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VFAT/ReadsSec',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VFAT/WritesSec',nil,regtype,@tmp,@vsize); <br>&nbsp; <br>&nbsp; &nbsp; &nbsp;//For VMM <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgLockedNoncache',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgCommit',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgSharedPages',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgDiskcacheMid',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgDiskcacheMac',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgDiskcacheMin',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgDiskcache',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgSwapfileDefective',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgSwapfileInUse',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgSwapfile',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cDiscards',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cPageOuts',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cPageIns',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cInstanceFaults',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cPageFaults',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgOther',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgSwap',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgLocked',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'VMM/cpgFree',nil,regtype,@tmp,@vsize); <br>&nbsp; <br>&nbsp; &nbsp; &nbsp;//For KERNEL <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'KERNEL/CPUUsage',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'KERNEL/VMs',nil,regtype,@tmp,@vsize); <br>&nbsp; &nbsp; &nbsp;RegQueryValueEx(pkey,'KERNEL/Threads',nil,regtype,@tmp,@vsize); <br>&nbsp; <br>&nbsp; &nbsp; &nbsp;RegCloseKey(pkey); <br>&nbsp; &nbsp; &nbsp;ftimer.enabled:=false; <br>&nbsp; &nbsp; &nbsp;fstopped:=true; <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure tsysinfo.fsetupdateinterval(aupdateinterval:integer); <br>&nbsp; begin <br>&nbsp; if (ftimer&lt;&gt;nil) and(aupdateinterval&gt;0) then <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; ftimer.Interval:=aupdateinterval; <br>&nbsp; &nbsp; fupdateinterval:=aupdateinterval; <br>&nbsp; end; <br>&nbsp; if (ftimer&lt;&gt;nil) and(aupdateinterval=0) then <br>&nbsp; begin <br>&nbsp; &nbsp; ftimer.Interval:=500; <br>&nbsp; &nbsp; fupdateinterval:=500; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; destructor tsysinfo.Destroy; <br>&nbsp; begin <br>&nbsp; StopRecievingInfo; <br>&nbsp; ftimer.Destroy; <br>&nbsp; inherited; <br>&nbsp; end; <br>&nbsp; <br>&nbsp; procedure Register; <br>&nbsp; begin <br>&nbsp; &nbsp; RegisterComponents('Samples', [TSysInfo]); <br>&nbsp; end; <br><br>&nbsp;<br>
 
请问有简单一点的方法吗?我只想获得cpu的温度和使用率
 
后退
顶部