Uses MemoryCpuUtils;//首先引用该单元  

//声明下列变量用来存储读取的数值
Var
iTotalPhysics, iTotalVirtual, iTotalPageFile,
iCurPhysics, iCurVirtual, iCurPageFile : DWord; //CPU数量
Format('系统中共有 %d 个CPU中央存储器',[GetCPUCount]);
//其中i表示获取第几个CPU的使用率
Format('CPU使用率为 #%d - %5.2f%%',[i,GetCPUUsage(i)*]);
//初始化OR读取各内存总量
GetMemoryTotalSize(iTotalPhysics ,iTotalVirtual ,iTotalPageFile);
//物理内存总量
InttoStr(Round(iTotalPhysics//)) + ' MB';
//虚拟内存总量
InttoStr(Round(iTotalVirtual//)) + ' MB';
//页面内存(交换内存)总量
InttoStr(Round(iTotalPageFile//)) + ' MB';
//读取各内存当前可用容量
GetMemoryTotalSize(iCurPhysics,iCurVirtual,iCurPageFile);
//物理内存可用容量
InttoStr(Round(iCurPhysics//)) + ' MB';
//虚拟内存可用容量
InttoStr(Round(iCurVirtual//)) + ' MB';
//页面内存(交换内存)可用容量
InttoStr(Round(iCurPageFile//)) + ' MB';
//获取内存使用率
Format('当前内存使用率为 %5.2f%%',[GetMemoryUsage]);
//直接获取CPU厂商
GetCPUVendor 那么如何计算当前各内存使用比例呢?
//物理内存使用比
Format('物理内存使用比为 %5.2f%%',[iCurPhysics/iTotalPhysics*]);
//虚拟内存使用比
Format('虚拟内存使用比为 %5.2f%%',[iCurVirtual/iTotalVirtual*]);
//页面内存使用比
Format('页面内存使用比为 %5.2f%%',[iCurPageFile/iTotalPageFile*]);

下载后直接引用即可,如果您还不是会员,可以直接COPY下面的代码。

{==================================================================}
{ Get Memory And Cpu Informations Utils }
{==================================================================} Unit MemoryCpuUtils; interface Uses
Windows, SysUtils; type
TVendor = array[..] of Char; {内存区}
//获取物理内存、虚拟内存、交换区(页面)内存的总容量,做初始化动作。
procedure GetMemoryTotalSize(Var iPhysicsMemoryTotalSize,
iVirtualMemoryTotalSize,
iPageFileMemoryTotalSize : DWORD); //获取当前物理内存、虚拟内存、交换区(页面)内存的实时可用容量,做监控显示动作。
procedure GetMemoryCurrentSize(Var iPhysicsMemoryCurrentSize,
iVirtualMemoryCurrentSize,
iPageFileMemoryCurrentSize : DWORD); //返回内存当前使用率 总的是100%,传回的是0-%间的使用率,可以自己做转换。
function GetMemoryUsage : Double; {CPU区}
//刷新CPU数据
procedure CollectCPUData; //获取CPU在系统中的总数
function GetCPUCount: Integer; //获取CPU使用率
function GetCPUUsage(Index: Integer): Double; procedure ReleaseCPUData; //获取CPU制造厂商
function GetCPUVendor : TVendor; assembler; register; implementation {$ifndef ver110} {$ifndef ver90}
{$ifndef ver100}
{$define UseInt64}
{$endif}
{$endif} {$ifdef UseInt64}
type TInt64 = Int64;
{$else}
type TInt64 = Comp;
{$endif} {$else} type TInt64 = TLargeInteger; {$endif} type
PInt64 = ^TInt64; type
TPERF_DATA_BLOCK = record
Signature : array[.. - ] of WCHAR;
LittleEndian : DWORD;
Version : DWORD;
Revision : DWORD;
TotalByteLength : DWORD;
HeaderLength : DWORD;
NumObjectTypes : DWORD;
DefaultObject : Longint;
SystemTime : TSystemTime;
Reserved: DWORD;
PerfTime : TInt64;
PerfFreq : TInt64;
PerfTime100nSec : TInt64;
SystemNameLength : DWORD;
SystemNameOffset : DWORD;
end; PPERF_DATA_BLOCK = ^TPERF_DATA_BLOCK; TPERF_OBJECT_TYPE = record
TotalByteLength : DWORD;
DefinitionLength : DWORD;
HeaderLength : DWORD;
ObjectNameTitleIndex : DWORD;
ObjectNameTitle : LPWSTR;
ObjectHelpTitleIndex : DWORD;
ObjectHelpTitle : LPWSTR;
DetailLevel : DWORD;
NumCounters : DWORD;
DefaultCounter : Longint;
NumInstances : Longint;
CodePage : DWORD;
PerfTime : TInt64;
PerfFreq : TInt64;
end; PPERF_OBJECT_TYPE = ^TPERF_OBJECT_TYPE; type
TPERF_COUNTER_DEFINITION = record
ByteLength : DWORD;
CounterNameTitleIndex : DWORD;
CounterNameTitle : LPWSTR;
CounterHelpTitleIndex : DWORD;
CounterHelpTitle : LPWSTR;
DefaultScale : Longint;
DetailLevel : DWORD;
CounterType : DWORD;
CounterSize : DWORD;
CounterOffset : DWORD;
end; PPERF_COUNTER_DEFINITION = ^TPERF_COUNTER_DEFINITION; TPERF_COUNTER_BLOCK = record
ByteLength : DWORD;
end; PPERF_COUNTER_BLOCK = ^TPERF_COUNTER_BLOCK; TPERF_INSTANCE_DEFINITION = record
ByteLength : DWORD;
ParentObjectTitleIndex : DWORD;
ParentObjectInstance : DWORD;
UniqueID : Longint;
NameOffset : DWORD;
NameLength : DWORD;
end; PPERF_INSTANCE_DEFINITION = ^TPERF_INSTANCE_DEFINITION; {$ifdef ver130}
{$L-} // The L+ causes internal error in Delphi compiler
{$O-} // The O+ causes internal error in Delphi compiler
{$Y-} // The Y+ causes internal error in Delphi compiler
{$endif} {$ifndef ver110}
type
TInt64F = TInt64;
{$else}
type
TInt64F = Extended;
{$endif} {$ifdef ver110}
function FInt64(Value: TInt64): TInt64F;
function Int64D(Value: DWORD): TInt64;
{$else}
type
FInt64 = TInt64F;
Int64D = TInt64;
{$endif} {$ifdef ver110}
function FInt64(Value: TInt64): TInt64F;
Var
V: TInt64;
begin
if (Value.HighPart and $) = then // positive value
begin
result:=Value.HighPart;
result:=result*$*$;
result:=result+Value.LowPart;
end
else
begin
V.HighPart:=Value.HighPart xor $FFFFFFFF;
V.LowPart:=Value.LowPart xor $FFFFFFFF;
result:= - - FInt64(V);
end;
end; function Int64D(Value: DWORD): TInt64;
begin
Result.LowPart:=Value;
Result.HighPart := ; // positive only
end;
{$endif} Const
Processor_IDX_Str = '';
Processor_IDX = ;
CPUUsageIDX = ; type
AInt64F = array[..$FFFF] of TInt64F;
PAInt64F = ^AInt64F; Var
_PerfData : PPERF_DATA_BLOCK;
_BufferSize: Integer;
_POT : PPERF_OBJECT_TYPE;
_PCD: PPerf_Counter_Definition;
_ProcessorsCount: Integer;
_Counters: PAInt64F;
_PrevCounters: PAInt64F;
_SysTime: TInt64F;
_PrevSysTime: TInt64F;
_IsWinNT: Boolean; _W9xCollecting: Boolean;
_W9xCpuUsage: DWORD;
_W9xCpuKey: HKEY; procedure GetMemoryTotalSize(Var iPhysicsMemoryTotalSize,
iVirtualMemoryTotalSize,
iPageFileMemoryTotalSize : DWORD);
{
iPhysicsMemoryTotalSize 物理内存总容量
iVirtualMemoryTotalSize 虚拟内存总容量
iPageFileMemoryTotalSize 交换内存(页面)总容量
}
Var
msMemory : TMemoryStatus;
begin
msMemory.dwLength := SizeOf(msMemory);
GlobalMemoryStatus(msMemory);
iPhysicsMemoryTotalSize := msMemory.dwTotalPhys;
iVirtualMemoryTotalSize := msMemory.dwTotalVirtual;
iPageFileMemoryTotalSize := msMemory.dwTotalPageFile;
end; procedure GetMemoryCurrentSize(Var iPhysicsMemoryCurrentSize,
iVirtualMemoryCurrentSize,
iPageFileMemoryCurrentSize : DWORD);
{
iPhysicsMemoryCurrentSize 物理内存可用容量
iVirtualMemoryCurrentSize 虚拟内存可用容量
iPageFileMemoryCurrentSize 交换内存(页面)可用容量
}
Var
msMemory : TMemoryStatus;
begin
msMemory.dwLength := SizeOf(msMemory);
GlobalMemoryStatus(msMemory);
iPhysicsMemoryCurrentSize := msMemory.dwAvailPhys;
iVirtualMemoryCurrentSize := msMemory.dwAvailVirtual;
iPageFileMemoryCurrentSize := msMemory.dwAvailPageFile;
end; function GetMemoryUsage : Double;
{
返回内存当前使用率 总的是100%,传回的是0-100%间的使用率,可以自己做转换。
}
Var
msMemory : TMemoryStatus;
begin
try
msMemory.dwLength := SizeOf(msMemory);
GlobalMemoryStatus(msMemory);
Result := msMemory.dwMemoryLoad;
except
Result := ;
end;
end; function GetCPUCount: Integer;
{
获取CPU数量
}
begin
if _IsWinNT then
begin
if _ProcessorsCount < then
CollectCPUData;
Result:=_ProcessorsCount;
end
else
begin
Result:=;
end;
end; procedure ReleaseCPUData;
Var
H: HKEY;
R: DWORD;
DwDataSize, DwType: DWORD;
begin
if _IsWinNT then Exit;
if Not _W9xCollecting then Exit;
_W9xCollecting := False;
RegCloseKey(_W9xCpuKey);
R := RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StopStat', , KEY_ALL_ACCESS, H);
if R <> ERROR_SUCCESS then Exit;
dwDataSize := Sizeof(DWORD);
RegQueryValueEx(H,'KERNEL/CPUUsage', Nil, @DwType, PBYTE(@_W9xCpuUsage), @DwDataSize);
RegCloseKey(H);
end; function GetCPUUsage(Index: Integer): Double;
{
获取CPU当前使用率
}
begin
if _IsWinNT then
begin
if _ProcessorsCount < then CollectCPUData;
if (Index >= _ProcessorsCount) Or (Index < ) then
Raise Exception.Create('CPU index out of bounds');
if _PrevSysTime = _SysTime then
Result :=
else
Result := -(_Counters[index] - _PrevCounters[index])/(_SysTime-_PrevSysTime);
end
else
begin
if Index <> then
Raise Exception.Create('CPU index out of bounds');
if Not _W9xCollecting then
CollectCPUData;
Result := _W9xCpuUsage/;
end;
end; Var
VI: TOSVERSIONINFO; procedure CollectCPUData;
Var
BS, i : Integer;
_PCB_Instance : PPERF_COUNTER_BLOCK;
_PID_Instance : PPERF_INSTANCE_DEFINITION;
ST : TFileTime;
H : HKEY;
R : DWORD;
DwDataSize, dwType: DWORD;
begin
if _IsWinNT then
begin
BS:=_BufferSize;
while RegQueryValueEx( HKEY_PERFORMANCE_DATA, Processor_IDX_Str, nil, nil,
PByte(_PerfData), @BS ) = ERROR_MORE_DATA do
begin
INC(_BufferSize,$);
BS:=_BufferSize;
ReallocMem( _PerfData, _BufferSize );
end; _POT := PPERF_OBJECT_TYPE(DWORD(_PerfData) + _PerfData.HeaderLength);
for i := to _PerfData.NumObjectTypes do
begin
if _POT.ObjectNameTitleIndex = Processor_IDX then Break;
_POT := PPERF_OBJECT_TYPE(DWORD(_POT) + _POT.TotalByteLength);
end; if _POT.ObjectNameTitleIndex <> Processor_IDX then
Raise Exception.Create('Unable to locate the "Processor" performance object'); if _ProcessorsCount < then
begin
_ProcessorsCount:=_POT.NumInstances;
GetMem(_Counters,_ProcessorsCount*SizeOf(TInt64));
GetMem(_PrevCounters,_ProcessorsCount*SizeOf(TInt64));
end; _PCD := PPERF_Counter_DEFINITION(DWORD(_POT) + _POT.HeaderLength);
for i := to _POT.NumCounters do
begin
if _PCD.CounterNameTitleIndex = CPUUsageIDX then Break;
_PCD := PPERF_COUNTER_DEFINITION(DWORD(_PCD) + _PCD.ByteLength);
end; if _PCD.CounterNameTitleIndex <> CPUUsageIDX then
Raise Exception.Create('Unable to locate the "% of CPU usage" performance counter'); _PID_Instance := PPERF_INSTANCE_DEFINITION(DWORD(_POT) + _POT.DefinitionLength);
for i := to _ProcessorsCount- do
begin
_PCB_Instance := PPERF_COUNTER_BLOCK(DWORD(_PID_Instance) + _PID_Instance.ByteLength);
_PrevCounters[i]:=_Counters[i];
_Counters[i]:=FInt64(PInt64(DWORD(_PCB_Instance) + _PCD.CounterOffset)^);
_PID_Instance := PPERF_INSTANCE_DEFINITION(DWORD(_PCB_Instance) + _PCB_Instance.ByteLength);
end; _PrevSysTime:=_SysTime;
SystemTimeToFileTime(_PerfData.SystemTime, ST);
_SysTime:=FInt64(TInt64(ST));
end
else
begin
if Not _W9xCollecting then
begin
R:=RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StartStat', , KEY_ALL_ACCESS, H );
if R <> ERROR_SUCCESS then
Raise Exception.Create('Unable to start performance monitoring');
dwDataSize:=sizeof(DWORD);
RegQueryValueEx( H, 'KERNEL/CPUUsage', nil, @dwType, PBYTE(@_W9xCpuUsage), @dwDataSize );
RegCloseKey(H);
R:=RegOpenKeyEx( HKEY_DYN_DATA, 'PerfStats/StatData', ,KEY_READ, _W9xCpuKey );
if R <> ERROR_SUCCESS then
Raise Exception.Create('Unable to read performance data');
_W9xCollecting:=True;
end; dwDataSize:=sizeof(DWORD);
RegQueryValueEx( _W9xCpuKey, 'KERNEL/CPUUsage', nil,@dwType, PBYTE(@_W9xCpuUsage), @dwDataSize );
end;
end; function GetCPUVendor : TVendor; assembler; register;
asm
PUSH EBX {Save affected register}
PUSH EDI
MOV EDI,EAX {@Result (TVendor)}
MOV EAX,
DW $A20F {CPUID Command}
MOV EAX,EBX
XCHG EBX,ECX {save ECX result}
MOV ECX,
@:
STOSB
SHR EAX,
LOOP @
MOV EAX,EDX
MOV ECX,
@:
STOSB
SHR EAX,
LOOP @
MOV EAX,EBX
MOV ECX,
@:
STOSB
SHR EAX,
LOOP @
POP EDI {Restore registers}
POP EBX
end; initialization
_ProcessorsCount:= -;
_BufferSize:= $;
_PerfData := AllocMem(_BufferSize);
VI.dwOSVersionInfoSize := SizeOf(VI);
if Not GetVersionEx(VI) then
Raise Exception.Create('Can''t get the Windows version');
_IsWinNT := VI.dwPlatformId = VER_PLATFORM_WIN32_NT; finalization
ReleaseCPUData;
FreeMem(_PerfData); end.

Delphi 获取内存及CPU信息的函数的更多相关文章

  1. linux获取内存、cpu、负载、网口流量、磁盘信息

    内存信息 / meminfo 返回dict   #!/usr/bin/env python   def memory_stat():   mem = {}   f = open("/proc ...

  2. 查询系统状态 内存大小 cpu信息 设备负载情况

    1.1 查看内存状态 /proc/meminfo里面存放着内存的信息 查看内存命令(包括虚拟内存swap): free -h (低版本系统可能不支持-h) 或者 free -m (以mb单位显示) a ...

  3. 获取Exe文件版本信息的函数(使用GetFileVersionInfo得到TFileVersionInfo结构体,包含12项内容)

    Type   TFileVersionInfo   =   Record         FixedInfo:TVSFixedFileInfo;         {版本信息}         Comp ...

  4. Delphi获取其他exe程序版本号

    delphi获取Exe文件版本信息的函数 Type TFileVersionInfo = Record FixedInfo:TVSFixedFileInfo; {版本信息} CompanyName:S ...

  5. [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)

    github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...

  6. Golang利用第三方包获取本机cpu使用率以及内存使用情况

    第三方包下载 $ github.com/shirou/gopsutil 获取内存方面的信息 package main import ( "fmt" "github.com ...

  7. CPU测试--通过proc获取CPU信息

    adb shell cat /proc/stat | grep cpu > totalcpu0 此处第一行的数值表示的是CPU总的使用情况,所以我们只要用第一行的数字计算就可以了.下表解析第一行 ...

  8. MD5做为文件名。机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能获取吧。

    可以采用机器(电脑)唯一码 + 上传IP + 当前时间戳 + GUID ( + 随机数),然后MD5做为文件名.机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能 ...

  9. Java如何获取系统cpu、内存、硬盘信息

    1 概述 前段时间摸索在Java中怎么获取系统信息包括cpu.内存.硬盘信息等,刚开始使用Java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插 ...

随机推荐

  1. 全新安装免费的OS X Mavericks 10.9正式版--安装U盘制作指南

    承诺大家的教程来了,这个教程是介绍如何在Mac下制作安装正版USB启动安装程序,原教程出自Tonymacx86.我只是加入了自己的理解,用自己的方式给大家讲这个过程.这里要把所有的成绩归功于Tonym ...

  2. Mysql数据备份与恢复命令

    转载:原文地址 一.备份常用操作基本命令 1.备份命令mysqldump格式 格式:mysqldump -h主机名 -P端口 -u用户名 -p密码 –database 数据库名 > 文件名.sq ...

  3. PAT (Advanced Level) 1001. A+B Format (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  4. 关于配置Tomcat的URIEncoding

    遇到的问题: 程序需要发送http GET请求到服务器,请求的参数中包含了中文字符.程序中参数为UTF-8格式,且经过了UTF-8 URL编码再发送.使用的tomcat服务器,但服务器端后台程序中取到 ...

  5. 想要学习Linux技术,先好好的读一本Linux书籍吧

    忘记你在使用windows时的使用习惯和使用思维.学习Linux,一定要适应Linux的命令行界面,因为命令行才是Linux的真正魅力所在,而X-window或着说桌面环境也只是运行在命令行模式下的一 ...

  6. 【poj解题】3664

    简单,两次排序 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 500 ...

  7. 对于形式参数只能用final修饰符,其它任何修饰符都会引起编译器错误

    在Java中修饰符总共有一下几种: 1.访问控制修饰符    分别有:public private protected,缺省 2.其它修饰符      分别有:abstract,final,stati ...

  8. Delphi 中Format的字符串格式化使用说明(转)

    源:Delphi 中Format的字符串格式化使用说明(转) 一.Format函数的用法 Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供 ...

  9. AngularJs ng-class 使用

    今天在做项目的时候要对表格内的部分的最大最小值高亮 解决方案 1. 引用 ng-class 2. 引用原型求最大最小值 实例 AngularJs HTML 代码 <table class=&qu ...

  10. 手机端 H5上传头像

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...