抄的一段,版权不归我.<br>---------<br>interface<br><br>uses<br>Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>Dialogs, StdCtrls;<br><br>const<br>SystemBasicInformation = 0;<br>SystemPerformanceInformation = 2;<br>SystemTimeInformation = 3;<br><br>type<br>TPDWord = ^DWORD;<br><br>TSystem_Basic_Information = packed record<br>dwUnknown1: DWORD;<br>uKeMaximumIncrement: ULONG;<br>uPageSize: ULONG;<br>uMmNumberOfPhysicalPages: ULONG;<br>uMmLowestPhysicalPage: ULONG;<br>uMmHighestPhysicalPage: ULONG;<br>uAllocationGranularity: ULONG;<br>pLowestUserAddress: Pointer;<br>pMmHighestUserAddress: Pointer;<br>uKeActiveProcessors: ULONG;<br>bKeNumberProcessors: byte;<br>bUnknown2: byte;<br>wUnknown3: word;<br>end;<br><br>type<br>TSystem_Performance_Information = packed record<br>liIdleTime: LARGE_INTEGER; {LARGE_INTEGER}<br>dwSpare: array[0..75] of DWORD;<br>end;<br><br>type<br>TSystem_Time_Information = packed record<br>liKeBootTime: LARGE_INTEGER;<br>liKeSystemTime: LARGE_INTEGER;<br>liExpTimeZoneBias: LARGE_INTEGER;<br>uCurrentTimeZoneId: ULONG;<br>dwReserved: DWORD;<br>end;<br><br>var<br>NtQuerySystemInformation: function(infoClass: DWORD;<br>buffer: Pointer;<br>bufSize: DWORD;<br>returnSize: TPDword): DWORD; stdcall = nil;<br><br><br>liOldIdleTime: LARGE_INTEGER = ();<br>liOldSystemTime: LARGE_INTEGER = ();<br>SysBaseInfo: TSystem_Basic_Information;<br>SysPerfInfo: TSystem_Performance_Information;<br>SysTimeInfo: TSystem_Time_Information;<br>status: Longint; {long}<br>dbSystemTime: Double;<br>dbIdleTime: Double;<br>function GetCPUUsage
ouble;<br>implementation<br>function Li2Double(x: LARGE_INTEGER): Double;<br>begin<br>Result := x.HighPart * 4.294967296E9 + x.LowPart<br>end;<br><br>function GetCPUUsage
ouble;<br>var<br>bLoopAborted : boolean;<br>begin<br>if @NtQuerySystemInformation = nil then<br>NtQuerySystemInformation := GetProcAddress(GetModuleHandle(‘ntdll.dll‘),<br>‘NtQuerySystemInformation‘);<br>// get number of processors in the system<br>status := NtQuerySystemInformation(SystemBasicInformation, @SysBaseInfo, SizeOf(SysBaseInfo), nil);<br>if status <> 0 then Exit;<br>// Show some information<br>{with SysBaseInfo do<br>begin<br>ShowMessage(<br>Format(‘uKeMaximumIncrement: %d‘#13‘uPageSize: %d‘#13+<br>‘uMmNumberOfPhysicalPages: %d‘+#13+‘uMmLowestPhysicalPage: %d‘+#13+<br>‘uMmHighestPhysicalPage: %d‘+#13+‘uAllocationGranularity: %d‘#13+<br>‘uKeActiveProcessors: %d‘#13‘bKeNumberProcessors: %d‘,<br>[uKeMaximumIncrement, uPageSize, uMmNumberOfPhysicalPages,<br>uMmLowestPhysicalPage, uMmHighestPhysicalPage, uAllocationGranularity,<br>uKeActiveProcessors, bKeNumberProcessors]));<br>end;<br>}<br>bLoopAborted := False;<br>while not bLoopAborted do<br>begin<br>// get new system time<br>status := NtQuerySystemInformation(SystemTimeInformation, @SysTimeInfo, SizeOf(SysTimeInfo), 0);<br>if status <> 0 then Exit;<br>// get new CPU‘s idle time<br>status := NtQuerySystemInformation(SystemPerformanceInformation, @SysPerfInfo, SizeOf(SysPerfInfo), nil);<br>if status <> 0 then Exit;<br>// if it‘s a first call - skip it<br>if (liOldIdleTime.QuadPart <> 0) then<br>begin<br>// CurrentValue = NewValue - OldValue<br>dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);<br>dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);<br>// CurrentCpuIdle = IdleTime / SystemTime<br>dbIdleTime := dbIdleTime dbSystemTime;<br>// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors<br>dbIdleTime := 100.0 - dbIdleTime * 100.0 SysBaseInfo.bKeNumberProcessors + 0.5;<br>// Show Percentage<br>//Form1.Label1.Caption := FormatFloat(‘CPU Usage: 0.0 %‘,dbIdleTime);<br>//Application.ProcessMessages;<br>// Abort if user pressed ESC or Application is terminated<br>Result:=dbIdleTime;<br>bLoopAborted:=True;<br>//bLoopAborted := (GetKeyState(VK_ESCAPE) and 128 = 128) or Application.Terminated;<br>end;<br>// store new CPU‘s idle and