如何获取CPU的温度和使用率

zywuhao 2010-12-29 10:06:56
请问各位大侠,如何获取CPU的温度和使用率,最好有源码!不胜感激,
EMail: zywuhao@163.com
...全文
466 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
keeley20 2010-12-29
  • 打赏
  • 举报
回复
获取CPU的使用率

unit GetCPUUse;

interface

uses
Windows, SysUtils;

Type
TPDWord = ^DWORD;

TSystem_Basic_Information = packed record
dwUnknown1: DWORD;
uKeMaximumIncrement: ULONG;
uPageSize: ULONG;
uMmNumberOfPhysicalPages: ULONG;
uMmLowestPhysicalPage: ULONG;
uMmHighestPhysicalPage: ULONG;
uAllocationGranularity: ULONG;
pLowestUserAddress: Pointer;
pMmHighestUserAddress: Pointer;
uKeActiveProcessors: ULONG;
bKeNumberProcessors: byte;
bUnknown2: byte;
wUnknown3: word;
end;

Type
TSystem_Performance_Information = packed record
liIdleTime: LARGE_INTEGER; {LARGE_INTEGER}
dwSpare: array[0..75] of DWORD;
end;

Type
TSystem_Time_Information = packed record
liKeBootTime: LARGE_INTEGER;
liKeSystemTime: LARGE_INTEGER;
liExpTimeZoneBias: LARGE_INTEGER;
uCurrentTimeZoneId: ULONG;
dwReserved: DWORD;
end;

Var
NtQuerySystemInformation: function(infoClass: DWORD;
buffer: Pointer;
bufSize: DWORD;
returnSize: TPDword): DWORD; stdcall = nil;

liOldIdleTime: LARGE_INTEGER = ();
liOldSystemTime: LARGE_INTEGER = ();

Function GetCPUUsage: Double;

implementation

function Li2Double(x: LARGE_INTEGER): Double;
begin
Result := x.HighPart * 4.294967296E9 + x.LowPart
end;

Function GetCPUUsage: Double;
Var
SysBaseInfo : TSystem_Basic_Information;
SysPerfInfo : TSystem_Performance_Information;
SysTimeInfo : TSystem_Time_Information;
Status : LongInt;
dbSystemTime : Double;
dbIdleTime : Double;
Temp : String;

Begin
if @NtQuerySystemInformation = nil then
NtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll.dll'),
'NtQuerySystemInformation');

status := NtQuerySystemInformation(0, @SysBaseInfo, SizeOf(SysBaseInfo), nil);
if status <> 0 then
Exit;

status := NtQuerySystemInformation(3, @SysTimeInfo, SizeOf(SysTimeInfo), 0);
if status <> 0 then
Exit;

status := NtQuerySystemInformation(2, @SysPerfInfo, SizeOf(SysPerfInfo), nil);
if status <> 0 then
Exit;

if (liOldIdleTime.QuadPart <> 0) then
begin
dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);

dbIdleTime := dbIdleTime / dbSystemTime;

dbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors + 0.5;
End;

liOldIdleTime := SysPerfInfo.liIdleTime;
liOldSystemTime := SysTimeInfo.liKeSystemTime;

// wait one second

Result := dbIdleTime;
End;




end.

应用实例 StrToInt(floatTostr(round(GetCPUUsage)))

获取温度的 论坛有前辈发帖问过了 http://topic.csdn.net/t/20030604/08/1872894.html
zhuimeng321 2010-12-29
  • 打赏
  • 举报
回复
顶!!!!!!!!!
h98458 2010-12-29
  • 打赏
  • 举报
回复
CPU使用率:

unit CPUUsage;

interface
uses
Windows, SysUtils;
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;

function GetCPURate: Byte;

function NtQuerySystemInformation(
SystemInformationClass: UINT;
SystemInformation: Pointer;
SystemInformationLength: ULONG;
ReturnLength: PULONG): Integer; stdcall; external 'ntdll.dll';

var
FOldIdleTime: LARGE_INTEGER;
FOldSystemTime: LARGE_INTEGER;

implementation

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

end.

调用函数GetCPURate即得当前的使用率
gyk120 2010-12-29
  • 打赏
  • 举报
回复
我记得cnpack有个组件包,里面有相关的内容,楼主可以去找找

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧