如何使用Delphi获取其他进程的信息? [英] How can I get other processes' information with Delphi?
问题描述
我想制作一个显示以下信息的任务管理器程序:
I want to make a Task Manager program that displays this information:
- 图片名称
- 内存使用情况
- PID
我该怎么做?
推荐答案
您不需要J(WS)CL,因此有一个简单的WinAPI调用可以满足您的所有需求,这就是 CreateToolhelp32Snapshot
.要获取所有正在运行的进程的快照,必须按以下方式调用它:
You don't need the J(WS)CL therefore, there is a simple WinAPI call that does almost all you want, and this is CreateToolhelp32Snapshot
. To get a snapshot of all running processes, you have to call it as follows:
var
snapshot: THandle;
begin
snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
现在,您具有所有正在运行的进程的列表.您可以使用 Process32First
和 Process32Next
函数浏览此列表,列表条目为 PROCESSENTRY32
-结构(除其他外,还包含过程ID和图片名称).
Now you have a list of all running processes. You can navigate through this list with the Process32First
and Process32Next
functions, the list entries are PROCESSENTRY32
-structures (which contain, amongst others, the process ID and image name).
uses
Windows, TLHelp32, SysUtils;
var
snapshot: THandle;
ProcEntry: TProcessEntry32;
s: String;
begin
snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot <> INVALID_HANDLE_VALUE) then begin
ProcEntry.dwSize := SizeOf(ProcessEntry32);
if (Process32First(snapshot, ProcEntry)) then begin
s := ProcEntry.szExeFile;
// s contains image name of the first process
while Process32Next(snapshot, ProcEntry) do begin
s := ProcEntry.szExeFile;
// s contains image name of the current process
end;
end;
end;
CloseHandle(snapshot);
但是,似乎没有包含内存消耗信息,但是您可以通过另一个简单的API调用 GetProcessMemoryInfo
However, memory consumption information doesn't seem to be included, but you can get this via another simple API call, GetProcessMemoryInfo
uses
psAPI;
var
pmc: TProcessMemoryCounters;
begin
pmc.cb := SizeOf(pmc) ;
if GetProcessMemoryInfo(processID, @pmc, SizeOf(pmc)) then
// Usage in Bytes: pmc.WorkingSetSize
else
// fail
您只需要使用从快照中检索到的进程ID调用此函数.
You just have to call this function with the process IDs retrieved from the snapshot.
这篇关于如何使用Delphi获取其他进程的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!