您的位置: 网站首页 > 编程乐园 > 阅读文章

Delphi获取进程和进程ID(用ListView和ListBox分别实现)

  获取系统当前进程名和进程ID,注意在编写本单元时,应注意引用"TLHelp32"单元"use TLHelp32"。

Delphi获取系统进程和进程ID,ListBox实现:

Delphi获取进程

var
  Form1: TForm1;
  Summ: Word;

implementation

{$R *.dfm}

procedure TForm1.N2Click(Sender: TObject);
 var
  ContinueLoop: BOOL;
  NewItem: TListItem;
begin
  ListView1.Items.BeginUpdate;
  ListView1.Items.Clear;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  //CreateToolhelp32Snapshot函数得到进程快照
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32);  //初始化
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  //Process32First 得到一个系统快照里第一个进程的信息
  Summ := 0;
  while ContinueLoop do
    begin
    Summ := Summ + 1;
    NewItem := ListView1.Items.Add;   //在ListView1显示
    NewItem.ImageIndex := -1;
    NewItem.Caption := ExtractFileName(FProcessEntry32.szExeFile);//进程名称
    NewItem.subItems.Add(FormatFloat('00', Summ));//序号
    NewItem.subItems.Add(IntToStr(FProcessEntry32.th32ProcessID));//进程ID
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
  ListView1.Items.EndUpdate;
end;

Delphi获取系统进程和进程ID,ListBox实现:

Delphi获取进程

procedure TForm1.Button1Click(Sender: TObject);
var
  ProcessName : string; //进程名
  ProcessID  : integer; //进程表示符
  i : integer;
  ContinueLoop:BOOL;
  FSnapshotHandle:THandle; //进程快照句柄
  FProcessEntry32:TProcessEntry32; //进程入口的结构体信息
begin
  FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照
  FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
  ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程
  //循环例举
  while ContinueLoop  do
  begin
    ProcessName := FProcessEntry32.szExeFile;
    ProcessID := FProcessEntry32.th32ProcessID;
    Listbox.Items.add('应用程序名 :'+ProcessName +'#进程ID:'+ inttostr(ProcessID));
    ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
  end;
end;
  • 属于分类: 编程乐园
  • 本文标签:
  • 人气指数: 5,327
  • 文章作者: 野球小子
  • 生产日期: 2007年11月08日 - 11时29分15秒
看看还有没有您感兴趣的:

  • 您的大名(必填)
  • E-Mail (必填)
  • 您的网站(有的话就写一下吧~)
  • 评论内容:(必填)