【发布时间】:2014-07-20 13:47:03
【问题描述】:
我正在尝试编写一个小实用程序来检索计算机上正在运行的进程的列表以及每个进程正在使用的内存。
到目前为止,我有以下 Delphi 代码:
{ ******************************************************************
Return list of running processes
******************************************************************* }
procedure TGkrTools.GetProcList(var thelist : tstrings);
var
ExeName : string;
PSize : cardinal;
PID : cardinal;
TheLoop : boolean;
proc : PROCESSENTRY32;
hSnap : HWND;
pmc : PPROCESS_MEMORY_COUNTERS;
cb : integer;
begin
thelist.Clear; // Clear the list on entry
proc.dwSize := SizeOf(PROCESSENTRY32);//Give proc.dwSize the Size of bytes of PROCESSENTRY32
hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
TheLoop := Process32First(hSnap,proc);
while Integer(TheLoop) <> 0 do
begin
ExeName := ExtractFileName(proc.szExeFile); // Name of process executable
cb := SizeOf(_PROCESS_MEMORY_COUNTERS);
GetMem(pmc, cb);
pmc^.cb := cb;
PID:=proc.th32ProcessID;
if GetProcessMemoryInfo(PID, pmc, cb) then
begin
Psize:=pmc^.WorkingSetSize;
end
else
begin
Psize:=0;
end;
Thelist.Add(ExeName + ' ' + IntToStr(Psize)+ ' ' + IntToStr(PID));
TheLoop := Process32Next(hSnap,proc);//Looper is oposite Zero until there is a program to process by this function
end;
end;
我成功检索了“ExeName”和“PID”,但是当我执行 GetProcessMemoryInfo 函数时,调用返回“false”。
对我的代码有什么问题有任何想法吗? 我在 32 位 Windows 7 机器上运行它。
提前致谢,
古德芬努尔·克里斯蒂安森
【问题讨论】:
-
当
GetProcessMemoryInfo()得到 FALSE 时,GetLastError()会返回什么?