//类型声明
type
PProcessInfo = ^TProcessInfo;
TProcessInfo = Record
ExeFile : String;
ProcessID : DWORD;
end;
procedure EnumProcess(const AList : TList);
//枚举进程
var
p : pProcessInfo;
ContinueLoop : BOOL;
FSnapshotHandle : THandle;
FProcessEntry32 : TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
try
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
New(p);
p.ExeFile := FProcessEntry32.szExeFile;
p.ProcessID := FProcessEntry32.th32ProcessID;
AList.Add(p);
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
finally
CloseHandle(FSnapshotHandle);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ls:tList;
i:integer;
begin
ListBox1.Clear;
ls:=TList.Create;
try
EnumProcess(ls);
for i:=0 to ls.Count-1 do //你也可以不用显示出来,直接判断cmd.exe这个字符串是否在里面就可以了
ListBox1.Items.Add(PProcessInfo(ls.Items).ExeFile)
finally
ls.Free;
end;
end;