- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, TLHelp32, psapi;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- ListBox1: TListBox;
- procedure Button1Click(Sender: TObject);
- private
- function GetProcessPath(ProcessID: DWORD): string;
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- //------------------------------------------------------------------------------
- // 通过进程ID获得进程路径
- //------------------------------------------------------------------------------
- function TForm1.GetProcessPath(ProcessID: DWORD): string;
- var
- Hand: THandle;
- ModName: array[0..Max_Path - 1] of Char;
- hMod: HModule;
- n: DWORD;
- begin
- Result := '';
- Hand := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);
- if Hand > 0 then
- try
- ENumProcessModules(Hand, @hMod, Sizeof(hMod), n);
- if GetModuleFileNameEx(Hand, hMod, ModName, Sizeof(ModName)) > 0 then
- Result := ExtractFilePath(ModName);
- except
- end;
- end;
- 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;
- Listbox1.Items.add('应用程序名:' + ProcessName + '#进程ID:' + inttostr(ProcessID) + '路径:' + GetProcessPath(ProcessID));
- ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
- end;
- end;
- end.