Delphi获得当前所有进程名,进程PID(转)
标签:
it |
分类: delphi7 |
http://s15/mw690/94b1b400gx6CnJCTvYOfe&690
代码如下
//
获取系统当前进程名和进程ID,
//注:应引用"TLHelp32"单元"use TLHelp32"。
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, ComCtrls, StdCtrls,TLHelp32;
type
TForm2 = class(TForm)
private
public
end;
var
Form2: TForm2;
ContinueLoop:BOOL;
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
//在listview中显示进程
var
NewItem: TListItem;
Summ:integer;
begin
ListView1.Items.BeginUpdate;
ListView1.Items.Clear;
FSnapshotHandle := CreateToolhelp32Snapshot
//CreateToolhelp32Snapshot
ContinueLoop :=
Process32First(FSnapshotHandle, FProcessEntry32);
//Process32First
得到一个系统快照里第一个进程的信息
Summ :=
0;
while ContinueLoop do
end;
CloseHandle(FSnapshotHandle);
ListView1.Items.EndUpdate;
Label1.Caption:='进程数:'+inttostr(ListView1.Items.Count);
end;
procedure TForm2.Button2Click(Sender: TObject);
//在listbox中显示进程
id
var
ProcessName : string;
ProcessID : integer;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
//循环例举
while ContinueLoop do
begin
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
//对ListView1的初始化
ListView1.ViewStyle:=vsReport;
ListView1.Columns.Add;
ListView1.Column[0].Caption:='进程名';
ListView1.Column[0].AutoSize:=false;
ListView1.Column[0].Width:=100;
ListView1.Column[0].Alignment:=taLeftJustify;//左对齐
ListView1.Columns.Add;
ListView1.Column[ListView1.Columns.Count-1].Caption:='序号';
ListView1.Column[ListView1.Columns.Count-1].AutoSize:=true;
ListView1.Column[ListView1.Columns.Count-1].Alignment:=taLeftJustify;//左对齐
ListView1.Columns.Add;
ListView1.Column[ListView1.Columns.Count-1].Caption:='ID';
ListView1.Column[ListView1.Columns.Count-1].AutoSize:=true;
ListView1.Column[ListView1.Columns.Count-1].Alignment:=taLeftJustify;//左对齐
end;
end.
运行效果如下

加载中…