问题: 取进程信息??? ( 积分: 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
WORD ;
PrcInfo
ointer ;
PrcInfoLength:ULONG;
returnlength: TPDword
):
DWORD; stdcall ;external 'ntdll.dll' name 'NtQueryInformationProcess';
// 可以用动态调用(隐式装入)
....
// Our Function here:
function GetPrcVMCounters(PID
WORD):TStringList;
var
status
WORD;
retlen
WORD;
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'); // <--- 就是这个 [
]
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
问题讨论没有结束 ...