读书人

Windows7怎么获取系统的CPU使用率

发布时间: 2012-11-09 10:18:48 作者: rapoo

Windows7如何获取系统的CPU使用率?
以前是用 NtQuerySystemInformation的

查了下资料,是要用NtQuerySystemInformationEx了,但是加了点参数,不知道该如何调用了,请问有人实现过吗?

[解决办法]
微软不建议这么用,要是后续版本有修改,你还得改函数。
可以用GetSystemTimes比对两次的结果
[解决办法]
我这有一个函数是通过NtQuerySystemInformation获取的,你在WIN 7下试试:

Delphi(Pascal) code
function GetCPUUsage(var liOldIdleTime, liOldSystemTime: LARGE_INTEGER): string;type  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;  TSystem_Performance_Information = packed record    liIdleTime: LARGE_INTEGER; {LARGE_INTEGER}    dwSpare: array[0..75] of DWORD;  end;  TSystem_Time_Information = packed record    liKeBootTime: LARGE_INTEGER;    liKeSystemTime: LARGE_INTEGER;    liExpTimeZoneBias: LARGE_INTEGER;    uCurrentTimeZoneId: ULONG;    dwReserved: DWORD;  end;  TNtQuerySystemInformation = function(infoClass: DWORD; buffer: Pointer; bufSize: DWORD; var returnSize: Dword): DWORD; stdcall;var  NtQuerySystemInformation: TNtQuerySystemInformation;  SysBaseInfo: TSystem_Basic_Information;  SysPerfInfo: TSystem_Performance_Information;  SysTimeInfo: TSystem_Time_Information;  status: DWORD; {long}  dbSystemTime: Double;  dbIdleTime: Double;begin  Result := '<N/A>';  NtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll.dll'), 'NtQuerySystemInformation');  if @NtQuerySystemInformation = nil then Exit;    if (NtQuerySystemInformation(0, @SysBaseInfo, SizeOf(SysBaseInfo), status) <> 0)    or (NtQuerySystemInformation(3, @SysTimeInfo, SizeOf(SysTimeInfo), status) <> 0)    or (NtQuerySystemInformation(2, @SysPerfInfo, SizeOf(SysPerfInfo), status) <> 0) then Exit;  if liOldIdleTime.QuadPart <> 0 then  begin    // CurrentValue = NewValue - OldValue    dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);    dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);    // CurrentCpuIdle = IdleTime / SystemTime    dbIdleTime := dbIdleTime / dbSystemTime;    // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors    dbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors{ + 0.5};    // Show Percentage    Result := FormatFloat('0.00 %',dbIdleTime);  end;  liOldIdleTime := SysPerfInfo.liIdleTime;  liOldSystemTime := SysTimeInfo.liKeSystemTime;end;
[解决办法]
NtQuerySystemInformation是未导出函数,这个可能需要performance 相关的函数,.NET提供了相关的接口,但不知道原生API调用究竟是哪个函数
[解决办法]
关注学习下。
[解决办法]
可以获取每个CPU的使用率,XP下测试通过
http://blog.csdn.net/aqtata/archive/2010/06/13/5669697.aspx
[解决办法]
动手能力太差
试试SQLDebug_Fan的,
[解决办法]
探讨

引用:

可以获取每个CPU的使用率,XP下测试通过
http://blog.csdn.net/aqtata/archive/2010/06/13/5669697.aspx

谢谢,不过XP下的我已经有了....现在要找Win7下的

读书人网 >.NET

热点推荐