一个简单的读进程的类,进程列表结果在GetProcessID中
unit ProcessList;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Controls,
ExtCtrls, StdCtrls,Tlhelp32;
type
TProcessList=class(tpersistent)
Private
function GetProcessID:TStringList;//取得进程id
Published
property ProcessIDList:TStringList read GetProcessID;
end;
implementation
function TProcessList.GetProcessID:TStringList;//取得进程id
var
ContinueLoop:BOOL;
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
const
PROCESS_TERMINATE=$0001;
begin
result:=TstringList.create;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
{Result:=('th32ProcessId'+floattostr(FProcessEntry32.th32ProcessId)+' '+
'cntUsage '+ floattostr(FProcessEntry32.cntUsage)+' '+
'cntThreads '+ floattostr(FProcessEntry32.cntThreads)+' th32ParentProcessID '+
floattostr(FProcessEntry32.th32ParentProcessID)+' '+
FProcessEntry32.szExeFile);}
result.add(floattostr(FProcessEntry32.th32ProcessId)+ //'th32ProcessId'
','+FProcessEntry32.szExeFile);
while integer(ContinueLoop)<>0 do
begin
//Result:=Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,BOOL(0),FProcessEntry32.th32ProcessID),0));
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
if integer(ContinueLoop)<>0 then begin
{detailed
result:=result+('th32ProcessId'+floattostr(FProcessEntry32.th32ProcessId)+
',cntUsage '+floattostr(FProcessEntry32.cntUsage)+
',cntThreads '+ floattostr(FProcessEntry32.cntThreads)+
',th32ParentProcessID '+floattostr(FProcessEntry32.th32ParentProcessID)+
','+FProcessEntry32.szExeFile);}
result.add(floattostr(FProcessEntry32.th32ProcessId)+ //'th32ProcessId'
','+FProcessEntry32.szExeFile);
end;
end;
CloseHandle(FSnapshotHandle);
end;
end.