如何获得指定进程的CPU占用率?(100分)

  • 主题发起人 主题发起人 iikiki
  • 开始时间 开始时间
I

iikiki

Unregistered / Unconfirmed
GUEST, unregistred user!
RT 不要注册表那个 不好使
 
GetProcessTimes
 
const <br>&nbsp; SystemBasicInformation = 0; <br>&nbsp; SystemPerformanceInformation = 2; <br>&nbsp; SystemTimeInformation = 3; <br><br>type <br>&nbsp; TPDWord = ^DWORD; <br><br>&nbsp; TSystem_Basic_Information = packed record <br>&nbsp; &nbsp; dwUnknown1: DWORD; <br>&nbsp; &nbsp; uKeMaximumIncrement: ULONG; <br>&nbsp; &nbsp; uPageSize: ULONG; <br>&nbsp; &nbsp; uMmNumberOfPhysicalPages: ULONG; <br>&nbsp; &nbsp; uMmLowestPhysicalPage: ULONG; <br>&nbsp; &nbsp; uMmHighestPhysicalPage: ULONG; <br>&nbsp; &nbsp; uAllocationGranularity: ULONG; <br>&nbsp; &nbsp; pLowestUserAddress: Pointer; <br>&nbsp; &nbsp; pMmHighestUserAddress: Pointer; <br>&nbsp; &nbsp; uKeActiveProcessors: ULONG; <br>&nbsp; &nbsp; bKeNumberProcessors: byte; <br>&nbsp; &nbsp; bUnknown2: byte; <br>&nbsp; &nbsp; wUnknown3: word; <br>&nbsp; end; <br><br>type <br>&nbsp; TSystem_Performance_Information = packed record <br>&nbsp; &nbsp; liIdleTime: LARGE_INTEGER; {LARGE_INTEGER} <br>&nbsp; &nbsp; dwSpare: array[0..75] of DWORD; <br>&nbsp; end; <br><br>type <br>&nbsp; TSystem_Time_Information = packed record <br>&nbsp; &nbsp; liKeBootTime: LARGE_INTEGER; <br>&nbsp; &nbsp; liKeSystemTime: LARGE_INTEGER; <br>&nbsp; &nbsp; liExpTimeZoneBias: LARGE_INTEGER; <br>&nbsp; &nbsp; uCurrentTimeZoneId: ULONG; <br>&nbsp; &nbsp; dwReserved: DWORD; <br>&nbsp; end; <br><br>var <br>&nbsp; NtQuerySystemInformation: function(infoClass: DWORD; <br>&nbsp; &nbsp; buffer: Pointer; <br>&nbsp; &nbsp; bufSize: DWORD; <br>&nbsp; &nbsp; returnSize: TPDword): DWORD; stdcall = nil; <br><br><br>&nbsp; liOldIdleTime: LARGE_INTEGER = (); <br>&nbsp; liOldSystemTime: LARGE_INTEGER = (); <br><br>function Li2Double(x: LARGE_INTEGER): Double; <br>begin <br>&nbsp; Result := x.HighPart * 4.294967296E9 + x.LowPart <br>end; <br><br>procedure GetCPUUsage; <br>var <br>&nbsp; SysBaseInfo: TSystem_Basic_Information; <br>&nbsp; SysPerfInfo: TSystem_Performance_Information; <br>&nbsp; SysTimeInfo: TSystem_Time_Information; <br>&nbsp; status: Longint; {long} <br>&nbsp; dbSystemTime: Double; <br>&nbsp; dbIdleTime: Double; <br><br>&nbsp; bLoopAborted : boolean; <br><br>begin <br>&nbsp; if @NtQuerySystemInformation = nil then <br>&nbsp; &nbsp; NtQuerySystemInformation := GetProcAddress(GetModuleHandle('ntdll.dll'), <br>&nbsp; &nbsp; &nbsp; 'NtQuerySystemInformation'); <br><br>&nbsp; // get number of processors in the system <br><br>&nbsp; status := NtQuerySystemInformation(SystemBasicInformation, @SysBaseInfo, SizeOf(SysBaseInfo), nil); <br>&nbsp; if status &lt;&gt; 0 then Exit; <br><br>&nbsp; // Show some information <br>&nbsp; with SysBaseInfo do <br>&nbsp; begin <br>&nbsp; &nbsp; &nbsp; ShowMessage( <br>&nbsp; &nbsp; &nbsp; Format('uKeMaximumIncrement: %d'#13'uPageSize: %d'#13+ <br>&nbsp; &nbsp; &nbsp; 'uMmNumberOfPhysicalPages: %d'+#13+'uMmLowestPhysicalPage: %d'+#13+ <br>&nbsp; &nbsp; &nbsp; 'uMmHighestPhysicalPage: %d'+#13+'uAllocationGranularity: %d'#13+ <br>&nbsp; &nbsp; &nbsp; 'uKeActiveProcessors: %d'#13'bKeNumberProcessors: %d', <br>&nbsp; &nbsp; &nbsp; [uKeMaximumIncrement, uPageSize, uMmNumberOfPhysicalPages, <br>&nbsp; &nbsp; &nbsp; uMmLowestPhysicalPage, uMmHighestPhysicalPage, uAllocationGranularity, <br>&nbsp; &nbsp; &nbsp; uKeActiveProcessors, bKeNumberProcessors])); <br>&nbsp; end; <br><br><br>&nbsp; bLoopAborted := False; <br><br>&nbsp; while not bLoopAborted do <br>&nbsp; begin <br><br>&nbsp; &nbsp; // get new system time <br>&nbsp; &nbsp; status := NtQuerySystemInformation(SystemTimeInformation, @SysTimeInfo, SizeOf(SysTimeInfo), 0); <br>&nbsp; &nbsp; if status &lt;&gt; 0 then Exit; <br><br>&nbsp; &nbsp; // get new CPU's idle time <br>&nbsp; &nbsp; status := NtQuerySystemInformation(SystemPerformanceInformation, @SysPerfInfo, SizeOf(SysPerfInfo), nil); <br>&nbsp; &nbsp; if status &lt;&gt; 0 then Exit; <br><br>&nbsp; &nbsp; // if it's a first call - skip it <br>&nbsp; &nbsp; if (liOldIdleTime.QuadPart &lt;&gt; 0) then <br>&nbsp; &nbsp; begin <br><br>&nbsp; &nbsp; &nbsp; // CurrentValue = NewValue - OldValue <br>&nbsp; &nbsp; &nbsp; dbIdleTime := Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime); <br>&nbsp; &nbsp; &nbsp; dbSystemTime := Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime); <br><br>&nbsp; &nbsp; &nbsp; // CurrentCpuIdle = IdleTime / SystemTime <br>&nbsp; &nbsp; &nbsp; dbIdleTime := dbIdleTime / dbSystemTime; <br><br>&nbsp; &nbsp; &nbsp; // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors <br>&nbsp; &nbsp; &nbsp; dbIdleTime := 100.0 - dbIdleTime * 100.0 / SysBaseInfo.bKeNumberProcessors + 0.5; <br><br>&nbsp; &nbsp; &nbsp; // Show Percentage <br>&nbsp; &nbsp; &nbsp; Form1.Label1.Caption := FormatFloat('CPU Usage: 0.0 %',dbIdleTime); <br><br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages; <br><br>&nbsp; &nbsp; &nbsp; // Abort if user pressed ESC or Application is terminated <br>&nbsp; &nbsp; &nbsp; bLoopAborted := (GetKeyState(VK_ESCAPE) and 128 = 128) or Application.Terminated; <br><br>&nbsp; &nbsp; end; <br><br>&nbsp; &nbsp; // store new CPU's idle and system time <br>&nbsp; &nbsp; liOldIdleTime := SysPerfInfo.liIdleTime; <br>&nbsp; &nbsp; liOldSystemTime := SysTimeInfo.liKeSystemTime; <br><br>&nbsp; &nbsp; // wait one second <br>&nbsp; &nbsp; Sleep(1000); <br>&nbsp; end; <br>end; <br><br><br>procedure TForm1.Button1Click(Sender: TObject); <br>begin <br>&nbsp; GetCPUUsage <br>end;
 
唉,不知道是水平问题还是眼睛有问题,怎么贴这个代码,能达到楼主的要求吗?????
 
http://delphi.ktop.com.tw/board.php?cid=30&amp;fid=72&amp;tid=65225<br>有类似的问题,但不是很好的解决的方法;<br>还有使用PDH.DLL的相关函数<br>&nbsp;/Processor('进程')/% Processor Time
 
我懂了 谢谢。。。
 
后退
顶部