delphi根据进程号得到相关参数(100分)

  • 主题发起人 主题发起人 mzg421
  • 开始时间 开始时间
M

mzg421

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手好,我已经知道进程号怎么能够得到象windows任务管理器里的各项参数的值,包括cpu,cpu时间,内存使用情况,句柄等

谢谢给予回答
 
各位高手,怎么不回帖啊
 
问题: 取进程信息??? ( 积分: 10 )
分类: 系统相关

来自: zengyi, 时间: 2005-08-18 18:16:00, ID: 3171892
我程序已经能取到进程。,
但取进程占用资源情况,特别是CPU占用,内存使用,应该怎么取,谢谢???

来自: chenybin, 时间: 2005-08-18 20:56:51, ID: 3172091
网上找的,可能对你有用

http://www.delphibbs.com/delphibbs/dispq.asp?lid=1360311

Thanks duducat .PID -> 进程ID

下面是个用NtQueryInformationProcess的例子 : [ NT ,2K,XP ]
// 参考: Windows NT/2000 本机API (Gary Nebbett)
// Tested using Delphi 6.0, 7.0 On WinXP
type
PVM_Counters=^TVM_Counters;
TVM_Counters=record
PeakVirtualSize:ULONG;
VirtualSize:ULONG;
PagedFaultCount:ULONG;
PeakWorkingSetSize:ULONG;
WorkingSetSize:ULONG;
QuotaPeakPagedPoolUsage:ULONG;
QuotaPagedPoolUsage:ULONG;
QuotaPeakNonPagedPoolUsage:ULONG;
QuotaNonPagedPoolUsage:ULONG;
PagefileUsage:ULONG;
PeakPagefileUsage:ULONG;
end;
const ProcessVMCounters =3;
...
function NtQueryInformationProcess
(
ProcessHandle: Thandle;
PrcInfoClass:DWORD ;
PrcInfo:Pointer ;
PrcInfoLength:ULONG;
returnlength: TPDword
):
DWORD; stdcall ;external 'ntdll.dll' name 'NtQueryInformationProcess';
// 可以用动态调用(隐式装入)
....
// Our Function here:
function GetPrcVMCounters(PID:DWORD):TStringList;
var
status:DWORD;
retlen:DWORD;
VM_Info:TVM_Counters;
hProcess:THandle;
begin
result:=TStringList.Create;
hProcess :=OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,PID);
status:=NtQueryInformationProcess
(
hProcess,
ProcessVMCounters,
@VM_Info,
sizeof(TVM_Counters),
@retlen
);
if(status<>0) then
begin
ShowMessage('NtQueryInformationProcess 失败');
exit;
end;
with result do
begin
Add('进程虚拟地址空间的最大数值 : '+IntToStr(VM_Info.PeakVirtualSize)+' Byte');
Add('进程的虚拟地址空间的大小 : '+IntToStr(VM_Info.VirtualSize)+' Byte');
Add('进程分页错误数目 : '+IntToStr(VM_Info.PagedFaultCount)+' Byte');
Add('进程的工作集列表的最大值 : '+IntToStr(VM_Info.PeakWorkingSetSize)+' Byte');
Add('进程的工作集列表的大小 : '+IntToStr(VM_Info.WorkingSetSize)+' Byte'); // <--- 就是这个 [:D]
Add('填充到进程的分页池的峰值的最大值 : '+IntToStr(VM_Info.QuotaPeakPagedPoolUsage)+' Byte');
Add('填充到进程的分页池的峰值大小 : '+IntToStr(VM_Info.QuotaPagedPoolUsage)+' Byte');
Add('填充到进程的非分页池的峰值的最大值 : '+IntToStr(VM_Info.QuotaNonPagedPoolUsage)+' Byte');
Add('填充到进程的分页池的峰值大小 : '+IntToStr(VM_Info.QuotaNonPagedPoolUsage)+' Byte');
Add('进程多使用的页文件页的最大值 : '+IntToStr(VM_Info.PeakPagefileUsage)+' Byte');
end;
end;
// ----
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Lines:=GetPrcVMCounters(256);
end;

来自: zengyi, 时间: 2005-08-19 0:34:44, ID: 3172282
取CPU的呢,内存的,我刚刚已经取到了

来自: chenybin, 时间: 2005-08-20 2:55:33, ID: 3173785
看我留的哪个地址,里面有个C的代码

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef STATUS_INFO_LENGTH_MISMATCH
#define STATUS_INFO_LENGTH_MISMATCH ((DWORD )0xC0000004L)
#endif

typedef struct
{
FILETIME ftCreationTime;
DWORD dwUnknown1;
DWORD dwStartAddress;
DWORD dwOwningPID;
DWORD dwThreadID;
DWORD dwCurrentPriority;
DWORD dwBasePriority;
DWORD dwContextSwitches;
DWORD dwThreadState;
DWORD dwWaitReason;
DWORD dwUnknown2[5];
} THREADINFO, *PTHREADINFO;

#pragma warning( disable:4200 ) // Zero sized array
typedef struct
{
DWORD dwRelativeOffset; //相对偏移量
DWORD dwThreadCount; //线程数目
DWORD dwUnknown1[6];
FILETIME pfCreateTime;
DWORD dwUnknown2[5];
WCHAR* pszProcessName; //不带路径的名称
DWORD dwBasePriority; //优先级
DWORD dwProcessID; //进程ID
DWORD dwParentProcessID; //父进程ID
DWORD dwHandleCount; //句柄数目
DWORD dwUnknown3;
DWORD dwUnknown4;
DWORD dwVirtualBytesPeak;
DWORD dwTotalVirtualBytes;
DWORD dwPageFaults;
DWORD dwWorkingSetPeak;
DWORD dwTotalWorkingSet; //占用物理内存大小
DWORD dwPeakPagedPoolUsagePages;
DWORD dwTotalPagedPoolUsagePages;
DWORD dwPeakNonPagedPoolUsagePages;
DWORD dwTotalNonPagedPoolUsagePages;
DWORD dwPageFileBytesPeak;
DWORD dwPrivateBytes;
DWORD dwPageFileBytes;
DWORD dwUnknown7[4];
THREADINFO ti[0];
} _PROCESSINFO, *PPROCESSINFO;
#pragma warning( default:4200 )

DWORD (__stdcall *NtQuerySystemInformation )(ULONG,PVOID,ULONG,ULONG);

void ShowPI(void)
{
PBYTE pbyInfo = NULL;
PPROCESSINFO pProcessInfo;
DWORD dwSize = 0x4000;
WCHAR *pname;

if (!NtQuerySystemInformation)
NtQuerySystemInformation=(DWORD (__stdcall*)(ULONG,PVOID,ULONG,ULONG))

GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation");

pbyInfo=(PBYTE) malloc(dwSize);

if (pbyInfo)
{
for(;;)
{
memset(pbyInfo,0,dwSize);


if(NtQuerySystemInformation(5,pbyInfo,dwSize,0)!=STATUS_INFO_LENGTH_MISMATCH
)
break;

dwSize += 0x1000;

pbyInfo=(PBYTE) realloc(pbyInfo,dwSize);
}

pProcessInfo = ( PPROCESSINFO ) pbyInfo;

for(;;)
{
pname=pProcessInfo->pszProcessName;

if (!pname)
pname=(L"Idle");

printf("%-40ls %-12lu %12luK/n",
pname,
pProcessInfo->dwProcessID,
pProcessInfo->dwTotalWorkingSet/1024ul
);

if ( pProcessInfo->dwRelativeOffset == 0 )
break;


pProcessInfo=(PPROCESSINFO)((PBYTE)pProcessInfo+pProcessInfo->dwRelativeOffset);
}

free( pbyInfo );
}
}

int main(void)
{
ShowPI();
getchar();
return 0;
}

来自: zengyi, 时间: 2005-08-20 9:05:45, ID: 3173850
只有10分了,不好意思,谢谢
还有我要的到进程占用CPU资源的时间怎么不的形呢,显示出来全部是1961-06-01呢,用的是这个API GetProcessTimes

来自: chenybin, 时间: 2005-08-21 15:15:54, ID: 3174792
网上找的代码,其中Edit1.text里面放的是Pid(通过任务管理器可以找到)

procedure TForm1.Button1Click(Sender: TObject);
function GetTimeString(AFileTime: TFileTime): string;
var
ASysTime: TSystemTime;
begin
FileTimeToSystemTime(AFileTime, ASysTime);
with ASysTime do
Result := Format('%d-%d-%d %d:%d:%d.%d', [wYear, wMonth, wDay, wHour,
wMinute, wSecond, wMilliseconds]);
end;
var
lpCreationTime, lpExitTime, lpKernelTime, lpUserTime: TFileTime;
hProcess: THandle;
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION, False, StrToInt(Edit1.Text));
try
GetProcessTimes(hProcess, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime);
caption := IntTostr(GetLastError);
finally
CloseHandle(hProcess);
end;
Memo1.Lines.Add(GetTimeString(lpCreationTime));
Memo1.Lines.Add(GetTimeString(lpExitTime));
Memo1.Lines.Add(GetTimeString(lpKernelTime));
Memo1.Lines.Add(GetTimeString(lpUserTime));
end;

我的结果是
2005-8-21 4:33:30.234
1601-1-1 0:0:0.0
1601-1-1 0:0:10.359
1601-1-1 0:0:11.906
---------------------------
网上找的说明

GetProcessTimes

VB声明
Declare Function GetProcessTimes Lib "kernel32" Alias "GetProcessTimes" (ByVal hProcess As Long, lpCreationTime As FILETIME, lpExitTime As FILETIME, lpKernelTime As FILETIME, lpUserTime As FILETIME) As Long

说明
获取与一个进程的经过时间有关的信息
返回值
Long,非零表示成功,零表示失败。会设置GetLastError
参数表
参数 类型及说明
hProcess Long,一个进程句柄
lpCreationTime FILETIME,指定一个FILETIME结构,在其中装载进程的创建时间
lpExitTime FILETIME,指定一个FILETIME结构,在其中装载进程的中止时间
lpKernelTime FILETIME,指定一个FILETIME结构,在其中装载进程花在内核模式上的总时间
lpUserTime FILETIME,指定一个FILETIME结构,在其中装载进程花在用户模式上的总时间
适用平台
Windows NT

问题讨论没有结束 ...
 
问题: 请问哪个api函数可以得到进程的资源耗用? ( 积分: 50 )
分类: 系统相关

来自: Liulang, 时间: 2003-04-13 9:36:00, ID: 1763979
2k/xp的窗口控制台可以看到进程的内存,cpu占用情况,还有就是设置进程的优先级,不知道怎么实现的

来自: Liulang, 时间: 2003-04-13 13:38:00, ID: 1764385
ps:在win98下面

来自: ike, 时间: 2003-04-13 20:42:00, ID: 1765112
帮你找了好久,没有找到,自己下个API函数集慢慢瞧吧。

来自: 太阳火, 时间: 2003-04-13 23:41:00, ID: 1765421
unit adCpuUsage;

{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
CPU Usage Measurement routines for Delphi and C++ Builder

Author: Alexey A. Dynnikov
EMail: aldyn@chat.ru
WebSite: http://www.aldyn.ru/
Support: Use the e-mail aldyn@chat.ru
or support@aldyn.ru

Creation: Jul 8, 2000
Version: 1.02

Legal issues: Copyright (C) 2000 by Alexey A. Dynnikov <aldyn@chat.ru>

This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:

1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.

2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
USAGE:

1. Include this unit into project.

2. Call GetCPUCount to obtain the numbr of processors in the system

3. Each time you need to know the value of CPU usage call the CollectCPUData
to refresh the CPU usage information. Then call the GetCPUUsage to obtain
the CPU usage for given processor. Note that succesive calls of GetCPUUsage
without calling CollectCPUData will return the same CPU usage value.

Example:

procedure TTestForm.TimerTimer(Sender: TObject);
var i: Integer;
begin
CollectCPUData; // Get the data for all processors

for i:=0 to GetCPUCount-1 do // Show data for each processor
MInfo.Lines:=Format('CPU #%d - %5.2f%%',[i,GetCPUUsage(i)*100]);
end;
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

interface

uses
Windows, SysUtils;

// Call CollectCPUData to refresh information about CPU usage
procedure CollectCPUData;

// Call it to obtain the number of CPU's in the system
function GetCPUCount: Integer;

// Call it to obtain the % of usage for given CPU
function GetCPUUsage(Index: Integer): Double;

// For Win9x only: call it to stop CPU usage monitoring and free system resources
procedure ReleaseCPUData;

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[0..4 - 1] 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;


来自: 太阳火, 时间: 2003-04-13 23:43:00, ID: 1765424
再来转一个
问题提出/摘要:
从注册表获取如活动进程数、CPU占用率等信息?


回答:
在注册表中,有一个分支叫HKEY_DYN_DATA,大量的系统信息都保存在此分支下的PerfStats主键中。

为了获取如CPU的使用率等信息,你需要做三件事:

第一步是开始选择的计数器,可以通过读取相应的注册表主键完成。

例如:
如果我们想知道CPU的占用率,你可以读HKEY_DYN_DATA的这个主键
'PerfStats/StartStat/KERNEL/CPUusage'
这一步是开始计数器

下一步是是读取'PerfStats/StatData/KERNEL/CPUusage'的值,它的值给出了当前CPU的使用率。如果我们将代码放在一个Timer的事件中,我们就可以看到动态的变化了。

最后一步是停止计数器,通过读取注册表的'PerfStats/StopStat/KERNEL/CPUusage'键完成。

除CPU的占用率外,这里还有很多其它的计数器。你可以打开用注册表编辑器打开PerfStats/StatData键,可以看到全部的列表。

我生成了一个构件来获取注册表HKEY_DYN_DATA分支提供的所有计数的值。以下是代码:

unit SystemInfo;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,extctrls;

type TDialupAdapterInfo = record //Information of Dialup adapter
alignment:dword;
buffer:dword;
bytesrecieved:dword;
bytesXmit:dword;
ConnectSpeed:dword;
CRC:dword;
framesrecieved:dword;
FramesXmit:dword;
Framing:dword;
runts:dword;
Overrun:dword;
timeout:dword;
totalbytesrecieved:dword;
totalbytesXmit:dword;
end;

type TKernelInfo = record
CpuUsagePcnt:dword;
Numthreads:dword;
NumVMS:dword;
end;

type TVCACHEInfo = record
ccurpages:dword;
cMacPages:dword;
cminpages:dword;
FailedRecycles:dword;
Hits:dword;
LRUBuffers:dword;
LRURecycles:dword;
Misses:dword;
RandomRecycles:dword;
end;

type TFATInfo = record
BreadsSec:dword;
BwritesSec:dword;
Dirtydata:dword;
ReadsSec:dword;
WritesSec:dword;
end;

type TVMMInfo = record
CDiscards:dword;
CInstancefaults:dword;
CPageFaults:dword;
cPageIns:dword;
cPageOuts:dword;
cpgCommit:dword;
cpgDiskCache:dword;
cpgDiskCacheMac:dword;
cpgDiskCacheMid:dword;
cpgDiskCacheMin:dword;
cpgfree:dword;

cpglocked:dword;
cpglockedNoncache:dword;
cpgother:dword;
cpgsharedpages:dword;
cpgswap:dword;
cpgswapfile:dword;
cpgswapfiledefective:dword;
cpgswapfileinuse:dword;
end;

type
TSysInfo = class(TComponent)
private
fDialupAdapterInfo:TDialupAdapterInfo;
fKernelInfo:TKernelInfo;
fVCACHEInfo:TVCACHEInfo;
fFATInfo:TFATInfo;
fVMMInfo:TVMMInfo;
ftimer:TTimer;
fupdateinterval:integer;
tmp:dword;
vsize:dword;
pkey:hkey;
regtype:pdword;
fstopped:boolean;
procedure fupdatinginfo(sender:tobject);
procedure fsetupdateinterval(aupdateinterval:integer);
protected
fsysInfoChanged:TNotifyEvent;
public
constructor Create(Aowner:Tcomponent);override;
destructor Destroy;override;

property DialupAdapterInfo: TDialupAdapterInfo read fDialupAdapterInfo;
property KernelInfo: TKernelInfo read fKernelInfo;
property VCACHEInfo: TVCACHEInfo read fVCACHEInfo;
property FATInfo: TFATInfo read fFATInfo;
property VMMInfo: TVMMInfo read fVMMInfo;
procedure StartRecievingInfo;
procedure StopRecievingInfo;
published
property SysInfoChanged:TNotifyEvent read fsysInfoChanged write
fsysInfoChanged;//this event is called after a specific interval.
property UpdateInterval:integer read fupdateInterval write
fsetupdateinterval default 5000;
end;

procedure Register;

implementation

constructor TSysInfo.Create(Aowner:Tcomponent);
begin
inherited;
ftimer:=ttimer.Create(self);
ftimer.enabled:=false;
ftimer.OnTimer:=fupdatinginfo;
vsize:=4;
fstopped:=true;
end;

procedure TSysInfo.startrecievingInfo;
var
res:intege

来自: Liulang, 时间: 2004-04-16 8:46:11, ID: 2562300
接受答案了.

问题讨论没有结束 ...
 
后退
顶部