【Delphi】获取系统CPU使用率

来源:互联网 发布:跳跃网络官方 编辑:程序博客网 时间:2024/05/19 14:19

根据网上的代码稍作修改。需要先安装JwAPI

unit CPUUsage;interfaceuses  Windows, JwaNative;function GetCPURate: Byte;implementationvar  FOldIdleTime: LARGE_INTEGER;  FOldSystemTime: LARGE_INTEGER;function GetCPURate: Byte;var  PerfInfo: TSystemPerformanceInformation;  TimeInfo: TSystemTimeOfDayInformation;  BaseInfo: TSystemBasicInformation;  IdleTime: INT64;  SystemTime: INT64;begin  Result := 0;  if NtQuerySystemInformation(SystemTimeOfDayInformation, @TimeInfo, SizeOf(TimeInfo), nil) <> NO_ERROR then Exit;  if NtQuerySystemInformation(SystemPerformanceInformation, @PerfInfo, SizeOf(PerfInfo), nil) <> NO_ERROR then Exit;  if NtQuerySystemInformation(SystemBasicInformation, @BaseInfo, SizeOf(BaseInfo), nil) <> NO_ERROR then Exit;  if (FOldIdleTime.QuadPart <> 0) and (BaseInfo.NumberProcessors <> 0) then  begin    IdleTime := PerfInfo.IdleTime.QuadPart - FOldIdleTime.QuadPart;    SystemTime := TimeInfo.CurrentTime.QuadPart - FOldSystemTime.QuadPart;    if SystemTime <> 0 then      Result := Trunc(100.0 - (IdleTime / SystemTime) * 100.0 / BaseInfo.NumberProcessors);  end;  FOldIdleTime := PerfInfo.IdleTime;  FOldSystemTime := TimeInfo.CurrentTime;end;end.


原创粉丝点击