需要2个方法.高分.谢绝灌水 ( 积分: 300 )

  • 主题发起人 主题发起人 R_Baggio
  • 开始时间 开始时间
R

R_Baggio

Unregistered / Unconfirmed
GUEST, unregistred user!
1&nbsp;如何获得CPU利用率<br>2&nbsp;如何获得每秒磁盘读写数<br>请附上具体实现代码。300分。win2000下。
 
unit&nbsp;UnitCPU;<br><br>interface<br><br>uses<br>Windows,&nbsp;SysUtils;<br><br>//&nbsp;Call&nbsp;CollectCPUData&nbsp;to&nbsp;refresh&nbsp;information&nbsp;about&nbsp;CPU&nbsp;usage<br>procedure&nbsp;CollectCPUData;<br>//&nbsp;Call&nbsp;it&nbsp;to&nbsp;obtain&nbsp;the&nbsp;number&nbsp;of&nbsp;CPU's&nbsp;in&nbsp;the&nbsp;system<br>function&nbsp;GetCPUCount:&nbsp;integer;<br>//&nbsp;Call&nbsp;it&nbsp;to&nbsp;obtain&nbsp;the&nbsp;%&nbsp;of&nbsp;usage&nbsp;for&nbsp;given&nbsp;CPU<br>function&nbsp;GetCPUUsage(Index:&nbsp;integer):&nbsp;double;<br>//&nbsp;For&nbsp;Win9x&nbsp;only:&nbsp;call&nbsp;it&nbsp;to&nbsp;stop&nbsp;CPU&nbsp;usage&nbsp;monitoring&nbsp;and&nbsp;free&nbsp;system&nbsp;resources<br>procedure&nbsp;ReleaseCPUData;<br><br>implementation<br><br>type<br>PInt64&nbsp;=&nbsp;^TInt64;<br>TInt64&nbsp;=&nbsp;int64;<br><br>type<br>TPERF_DATA_BLOCK&nbsp;=&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;Signature:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[0..4&nbsp;-&nbsp;1]&nbsp;of&nbsp;WCHAR;<br>&nbsp;&nbsp;&nbsp;&nbsp;LittleEndian:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;Version:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;Revision:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;TotalByteLength:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;HeaderLength:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;NumObjectTypes:&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;DefaultObject:&nbsp;&nbsp;&nbsp;&nbsp;longint;<br>&nbsp;&nbsp;&nbsp;&nbsp;SystemTime:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TSystemTime;<br>&nbsp;&nbsp;&nbsp;&nbsp;Reserved:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;PerfTime:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TInt64;<br>&nbsp;&nbsp;&nbsp;&nbsp;PerfFreq:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TInt64;<br>&nbsp;&nbsp;&nbsp;&nbsp;PerfTime100nSec:&nbsp;TInt64;<br>&nbsp;&nbsp;&nbsp;&nbsp;SystemNameLength:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;SystemNameOffset:&nbsp;DWORD;<br>end;<br>PPERF_DATA_BLOCK&nbsp;=&nbsp;^TPERF_DATA_BLOCK;<br><br>TPERF_OBJECT_TYPE&nbsp;=&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;TotalByteLength:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;DefinitionLength:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;HeaderLength:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;ObjectNameTitleIndex:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;ObjectNameTitle:&nbsp;LPWSTR;<br>&nbsp;&nbsp;&nbsp;&nbsp;ObjectHelpTitleIndex:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;ObjectHelpTitle:&nbsp;LPWSTR;<br>&nbsp;&nbsp;&nbsp;&nbsp;DetailLevel:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;NumCounters:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;DefaultCounter:&nbsp;longint;<br>&nbsp;&nbsp;&nbsp;&nbsp;NumInstances:&nbsp;longint;<br>&nbsp;&nbsp;&nbsp;&nbsp;CodePage:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;PerfTime:&nbsp;TInt64;<br>&nbsp;&nbsp;&nbsp;&nbsp;PerfFreq:&nbsp;TInt64;<br>end;<br>PPERF_OBJECT_TYPE&nbsp;=&nbsp;^TPERF_OBJECT_TYPE;<br><br>type<br>TPERF_COUNTER_DEFINITION&nbsp;=&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;ByteLength:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;CounterNameTitleIndex:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;CounterNameTitle:&nbsp;LPWSTR;<br>&nbsp;&nbsp;&nbsp;&nbsp;CounterHelpTitleIndex:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;CounterHelpTitle:&nbsp;LPWSTR;<br>&nbsp;&nbsp;&nbsp;&nbsp;DefaultScale:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;longint;<br>&nbsp;&nbsp;&nbsp;&nbsp;DetailLevel:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;CounterType:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;CounterSize:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;CounterOffset:&nbsp;&nbsp;&nbsp;&nbsp;DWORD;<br>end;<br>PPERF_COUNTER_DEFINITION&nbsp;=&nbsp;^TPERF_COUNTER_DEFINITION;<br><br>TPERF_COUNTER_BLOCK&nbsp;=&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;ByteLength:&nbsp;DWORD;<br>end;<br>PPERF_COUNTER_BLOCK&nbsp;=&nbsp;^TPERF_COUNTER_BLOCK;<br><br>TPERF_INSTANCE_DEFINITION&nbsp;=&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;ByteLength:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;ParentObjectTitleIndex:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;ParentObjectInstance:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;UniqueID:&nbsp;&nbsp;&nbsp;longint;<br>&nbsp;&nbsp;&nbsp;&nbsp;NameOffset:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;NameLength:&nbsp;DWORD;<br>end;<br>PPERF_INSTANCE_DEFINITION&nbsp;=&nbsp;^TPERF_INSTANCE_DEFINITION;<br><br>type<br>TInt64F&nbsp;=&nbsp;TInt64;<br><br>type<br>FInt64&nbsp;=&nbsp;TInt64F;<br>Int64D&nbsp;=&nbsp;TInt64;<br>//------------------------------------------------------------------------------<br>const<br>Processor_IDX_Str&nbsp;=&nbsp;'238';<br>Processor_IDX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;238;<br>CPUUsageIDX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;6;<br><br>type<br>AInt64F&nbsp;=&nbsp;array[0..$FFFF]&nbsp;of&nbsp;TInt64F;<br>PAInt64F&nbsp;=&nbsp;^AInt64F;<br><br>var<br>_PerfData:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PPERF_DATA_BLOCK;<br>_BufferSize:&nbsp;&nbsp;&nbsp;&nbsp;integer;<br>_POT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PPERF_OBJECT_TYPE;<br>_PCD:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PPerf_Counter_Definition;<br>_ProcessorsCount:&nbsp;integer;<br>_Counters:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PAInt64F;<br>_PrevCounters:&nbsp;PAInt64F;<br>_SysTime:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TInt64F;<br>_PrevSysTime:&nbsp;&nbsp;&nbsp;TInt64F;<br>_IsWinNT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boolean;<br>_W9xCollecting:&nbsp;boolean;<br>_W9xCpuUsage:&nbsp;&nbsp;&nbsp;DWORD;<br>_W9xCpuKey:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HKEY;<br>//------------------------------------------------------------------------------<br>function&nbsp;GetCPUCount:&nbsp;integer;<br>begin<br>if&nbsp;_IsWinNT&nbsp;then<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;_ProcessorsCount&nbsp;&lt;&nbsp;0&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CollectCPUData;<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;_ProcessorsCount;<br>end&nbsp;else<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;1;<br>end;<br>end;<br>//------------------------------------------------------------------------------<br>procedure&nbsp;ReleaseCPUData;<br>var<br>H:&nbsp;HKEY;<br>R:&nbsp;DWORD;<br>dwDataSize,&nbsp;dwType:&nbsp;DWORD;<br>begin<br>if&nbsp;_IsWinNT&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;exit;<br>if&nbsp;not&nbsp;_W9xCollecting&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;exit;<br>_W9xCollecting&nbsp;:=&nbsp;False;<br>RegCloseKey(_W9xCpuKey);<br>R&nbsp;:=&nbsp;RegOpenKeyEx(HKEY_DYN_DATA,&nbsp;'PerfStats/StopStat',&nbsp;0,&nbsp;KEY_ALL_ACCESS,&nbsp;H);<br>if&nbsp;R&nbsp;&lt;&gt;&nbsp;ERROR_SUCCESS&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;exit;<br>dwDataSize&nbsp;:=&nbsp;sizeof(DWORD);<br>RegQueryValueEx(H,&nbsp;'KERNEL/CPUUsage',&nbsp;nil,&nbsp;@dwType,&nbsp;PBYTE(@_W9xCpuUsage),<br>&nbsp;&nbsp;&nbsp;&nbsp;@dwDataSize);<br>RegCloseKey(H);<br>end;<br>//------------------------------------------------------------------------------<br>function&nbsp;GetCPUUsage(Index:&nbsp;integer):&nbsp;double;<br>begin<br>if&nbsp;_IsWinNT&nbsp;then<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;_ProcessorsCount&nbsp;&lt;&nbsp;0&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CollectCPUData;<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(Index&nbsp;&gt;=&nbsp;_ProcessorsCount)&nbsp;or&nbsp;(Index&nbsp;&lt;&nbsp;0)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;Exception.Create('CPU&nbsp;index&nbsp;out&nbsp;of&nbsp;bounds');<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;_PrevSysTime&nbsp;=&nbsp;_SysTime&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;0<br>&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;1&nbsp;-&nbsp;(_Counters[index]&nbsp;-&nbsp;_PrevCounters[index])&nbsp;/&nbsp;(_SysTime&nbsp;-&nbsp;_PrevSysTime);<br>end&nbsp;else<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Index&nbsp;&lt;&gt;&nbsp;0&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;Exception.Create('CPU&nbsp;index&nbsp;out&nbsp;of&nbsp;bounds');<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;_W9xCollecting&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CollectCPUData;<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;_W9xCpuUsage&nbsp;/&nbsp;100;<br>end;<br>end;<br><br>var<br>VI:&nbsp;TOSVERSIONINFO;<br>//------------------------------------------------------------------------------<br>procedure&nbsp;CollectCPUData;<br>var<br>BS:&nbsp;integer;<br>i:&nbsp;integer;<br>_PCB_Instance:&nbsp;PPERF_COUNTER_BLOCK;<br>_PID_Instance:&nbsp;PPERF_INSTANCE_DEFINITION;<br>ST:&nbsp;TFileTime;<br>var<br>H:&nbsp;HKEY;<br>R:&nbsp;DWORD;<br>dwDataSize,&nbsp;dwType:&nbsp;DWORD;<br>begin<br>if&nbsp;_IsWinNT&nbsp;then<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;BS&nbsp;:=&nbsp;_BufferSize;<br>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;RegQueryValueEx(HKEY_PERFORMANCE_DATA,&nbsp;Processor_IDX_Str,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil,&nbsp;nil,&nbsp;PByte(_PerfData),&nbsp;@BS)&nbsp;=&nbsp;ERROR_MORE_DATA&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Get&nbsp;a&nbsp;buffer&nbsp;that&nbsp;is&nbsp;big&nbsp;enough.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Inc(_BufferSize,&nbsp;$1000);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BS&nbsp;:=&nbsp;_BufferSize;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReallocMem(_PerfData,&nbsp;_BufferSize);<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Locate&nbsp;the&nbsp;performance&nbsp;object<br>&nbsp;&nbsp;&nbsp;&nbsp;_POT&nbsp;:=&nbsp;PPERF_OBJECT_TYPE(DWORD(_PerfData)&nbsp;+&nbsp;_PerfData.HeaderLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;:=&nbsp;1&nbsp;to&nbsp;_PerfData.NumObjectTypes&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;_POT.ObjectNameTitleIndex&nbsp;=&nbsp;Processor_IDX&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_POT&nbsp;:=&nbsp;PPERF_OBJECT_TYPE(DWORD(_POT)&nbsp;+&nbsp;_POT.TotalByteLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Check&nbsp;for&nbsp;success<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;_POT.ObjectNameTitleIndex&nbsp;&lt;&gt;&nbsp;Processor_IDX&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;Exception.Create(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Unable&nbsp;to&nbsp;locate&nbsp;the&nbsp;&quot;Processor&quot;&nbsp;performance&nbsp;object');<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;_ProcessorsCount&nbsp;&lt;&nbsp;0&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ProcessorsCount&nbsp;:=&nbsp;_POT.NumInstances;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetMem(_Counters,&nbsp;_ProcessorsCount&nbsp;*&nbsp;SizeOf(TInt64));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetMem(_PrevCounters,&nbsp;_ProcessorsCount&nbsp;*&nbsp;SizeOf(TInt64));<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Locate&nbsp;the&nbsp;&quot;%&nbsp;CPU&nbsp;usage&quot;&nbsp;counter&nbsp;definition<br>&nbsp;&nbsp;&nbsp;&nbsp;_PCD&nbsp;:=&nbsp;PPERF_Counter_DEFINITION(DWORD(_POT)&nbsp;+&nbsp;_POT.HeaderLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;:=&nbsp;1&nbsp;to&nbsp;_POT.NumCounters&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;_PCD.CounterNameTitleIndex&nbsp;=&nbsp;CPUUsageIDX&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_PCD&nbsp;:=&nbsp;PPERF_COUNTER_DEFINITION(DWORD(_PCD)&nbsp;+&nbsp;_PCD.ByteLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Check&nbsp;for&nbsp;success<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;_PCD.CounterNameTitleIndex&nbsp;&lt;&gt;&nbsp;CPUUsageIDX&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;Exception.Create(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Unable&nbsp;to&nbsp;locate&nbsp;the&nbsp;&quot;%&nbsp;of&nbsp;CPU&nbsp;usage&quot;&nbsp;performance&nbsp;counter');<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Collecting&nbsp;coutners<br>&nbsp;&nbsp;&nbsp;&nbsp;_PID_Instance&nbsp;:=&nbsp;PPERF_INSTANCE_DEFINITION(DWORD(_POT)&nbsp;+&nbsp;_POT.DefinitionLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i&nbsp;:=&nbsp;0&nbsp;to&nbsp;_ProcessorsCount&nbsp;-&nbsp;1&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_PCB_Instance&nbsp;&nbsp;&nbsp;&nbsp;:=&nbsp;PPERF_COUNTER_BLOCK(DWORD(_PID_Instance)&nbsp;+<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_PID_Instance.ByteLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_PrevCounters&nbsp;:=&nbsp;_Counters;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_Counters&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:=&nbsp;FInt64(PInt64(DWORD(_PCB_Instance)&nbsp;+&nbsp;_PCD.CounterOffset)^);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_PID_Instance&nbsp;&nbsp;&nbsp;&nbsp;:=&nbsp;PPERF_INSTANCE_DEFINITION(DWORD(_PCB_Instance)&nbsp;+<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_PCB_Instance.ByteLength);<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;_PrevSysTime&nbsp;:=&nbsp;_SysTime;<br>&nbsp;&nbsp;&nbsp;&nbsp;SystemTimeToFileTime(_PerfData.SystemTime,&nbsp;ST);<br>&nbsp;&nbsp;&nbsp;&nbsp;_SysTime&nbsp;:=&nbsp;FInt64(TInt64(ST));<br>end&nbsp;else<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;_W9xCollecting&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R&nbsp;:=&nbsp;RegOpenKeyEx(HKEY_DYN_DATA,&nbsp;'PerfStats/StartStat',<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0,&nbsp;KEY_ALL_ACCESS,&nbsp;H);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;R&nbsp;&lt;&gt;&nbsp;ERROR_SUCCESS&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;Exception.Create('Unable&nbsp;to&nbsp;start&nbsp;performance&nbsp;monitoring');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwDataSize&nbsp;:=&nbsp;sizeof(DWORD);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RegQueryValueEx(H,&nbsp;'KERNEL/CPUUsage',&nbsp;nil,&nbsp;@dwType,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PBYTE(@_W9xCpuUsage),&nbsp;@dwDataSize);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RegCloseKey(H);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R&nbsp;:=&nbsp;RegOpenKeyEx(HKEY_DYN_DATA,&nbsp;'PerfStats/StatData',<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0,&nbsp;KEY_READ,&nbsp;_W9xCpuKey);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;R&nbsp;&lt;&gt;&nbsp;ERROR_SUCCESS&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;Exception.Create('Unable&nbsp;to&nbsp;read&nbsp;performance&nbsp;data');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_W9xCollecting&nbsp;:=&nbsp;True;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;dwDataSize&nbsp;:=&nbsp;sizeof(DWORD);<br>&nbsp;&nbsp;&nbsp;&nbsp;RegQueryValueEx(_W9xCpuKey,&nbsp;'KERNEL/CPUUsage',&nbsp;nil,&nbsp;@dwType,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PBYTE(@_W9xCpuUsage),&nbsp;@dwDataSize);<br>end;<br>end;<br><br>initialization<br>_ProcessorsCount&nbsp;:=&nbsp;-1;<br>_BufferSize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:=&nbsp;$2000;<br>_PerfData&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:=&nbsp;AllocMem(_BufferSize);<br>VI.dwOSVersionInfoSize&nbsp;:=&nbsp;SizeOf(VI);<br>if&nbsp;not&nbsp;GetVersionEx(VI)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;raise&nbsp;Exception.Create('Can''t&nbsp;get&nbsp;the&nbsp;Windows&nbsp;version');<br>_IsWinNT&nbsp;:=&nbsp;VI.dwPlatformId&nbsp;=&nbsp;VER_PLATFORM_WIN32_NT;<br><br>finalization<br>ReleaseCPUData;<br>FreeMem(_PerfData);<br>end.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部