---
title: "获取 CPU 使用率 "
author: "cfeers"
date: "2011-05-12T16:53:51Z"
tags: []
categories: ["编程语言"]
slug: "cpu"
draft: false
lastmod: "2011-05-12T16:53:51Z"
id: 1041541
url: "https://cfeers.iteye.com/blog/1041541"
---

获取 CPU 使用率

![](http://hiphotos.baidu.com/e__jin/pic/item/bba187ec7f48041563d09f69.jpg)  
unit Unit1;

interface

uses  
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
Dialogs, StdCtrls, ExtCtrls, Gauges;

type  
\_SYSTEM\_PERFORMANCE\_INFORMATION = record  
    IdleTime: LARGE\_INTEGER;  
    Reserved: array\[0..75] of DWORD;  
end;  
PSystemPerformanceInformation = ^TSystemPerformanceInformation;  
TSystemPerformanceInformation = \_SYSTEM\_PERFORMANCE\_INFORMATION;

\_SYSTEM\_BASIC\_INFORMATION = record  
    Reserved1: array\[0..23] of Byte;  
    Reserved2: array\[0..3] of Pointer;  
    NumberOfProcessors: UCHAR;  
end;  
PSystemBasicInformation = ^TSystemBasicInformation;  
TSystemBasicInformation = \_SYSTEM\_BASIC\_INFORMATION;

\_SYSTEM\_TIME\_INFORMATION = record  
    KeBootTime: LARGE\_INTEGER;  
    KeSystemTime: LARGE\_INTEGER;  
    ExpTimeZoneBias: LARGE\_INTEGER;  
    CurrentTimeZoneId: ULONG;  
end;  
PSystemTimeInformation = ^TSystemTimeInformation;  
TSystemTimeInformation = \_SYSTEM\_TIME\_INFORMATION;

type  
TForm1 = class(TForm)  
    Gauge1: TGauge;  
    Timer1: TTimer;  
    procedure Button1Click(Sender: TObject);  
    procedure Timer1Timer(Sender: TObject);  
private  
    { Private declarations }  
public  
    { Public declarations }  
end;  
function NtQuerySystemInformation(  
SystemInformationClass: UINT;  
SystemInformation: Pointer;  
SystemInformationLength: ULONG;  
ReturnLength: PULONG): Integer; stdcall; external 'ntdll.dll';

var  
FOldIdleTime: LARGE\_INTEGER;  
FOldSystemTime: LARGE\_INTEGER;  
Form1: TForm1;

implementation

{$R \*.dfm}

function GetCPURate: Byte;  
var  
PerfInfo: TSystemPerformanceInformation;  
TimeInfo: TSystemTimeInformation;  
BaseInfo: TSystemBasicInformation;  
IdleTime: INT64;  
SystemTime: INT64;  
begin  
Result := 0;  
if NtQuerySystemInformation(3, @TimeInfo, SizeOf(TimeInfo), nil) &lt;&gt; NO\_ERROR then  
    Exit;  
if NtQuerySystemInformation(2, @PerfInfo, SizeOf(PerfInfo), nil) &lt;&gt; NO\_ERROR then  
    Exit;  
if NtQuerySystemInformation(0, @BaseInfo, SizeOf(BaseInfo), nil) &lt;&gt; NO\_ERROR then  
    Exit;  
if (FOldIdleTime.QuadPart &lt;&gt; 0) and (BaseInfo.NumberOfProcessors &lt;&gt; 0) then  
begin  
    IdleTime := PerfInfo.IdleTime.QuadPart - FOldIdleTime.QuadPart;  
    SystemTime := TimeInfo.KeSystemTime.QuadPart - FOldSystemTime.QuadPart;  
    if SystemTime &lt;&gt; 0 then  
      Result := Trunc(100.0 - (IdleTime / SystemTime) * 100.0 / BaseInfo.NumberOfProcessors);  
end;  
FOldIdleTime := PerfInfo.IdleTime;  
FOldSystemTime := TimeInfo.KeSystemTime;  
end;

procedure TForm1.Button1Click(Sender: TObject);  
begin  
self.Caption := inttostr(GetCPURate);  
end;

procedure TForm1.Timer1Timer(Sender: TObject);  
begin  
gauge1.Progress := GetCPURate;  
end;

end.