取得进程和dll

     前些时候看到别人写出了这样的一个小程序:取得系统所有的进程名和其相应的dll路径,其实是很容易的,只要你懂得几个函数:

   CreateToolhelp32Snapshot, Module32First, Module32Next,Process32First,Process32Next, 呵呵 你了解这些东西你就会可以写出来了。

    主要的代码如下:

记得引用:Winapi.TlHelp32, Winapi.PsAPI,
其中: List: array [0..254,0..1] of Cardinal;为全局

procedure TForm1.FormShow(Sender: TObject);
begin
// cxGrid1DBTableView1.Columns[0].Visible := False;
// cxGrid1DBTableView1.Columns[0].SortOrder := soAscending;
//cxGrid1DBTableView1.Columns[0].GroupIndex := 0;
end;

function TForm1.GetList: Boolean; //取得进程列表
var
i: Integer;
Lpp: TProcessEntry32;
handle: THandle;
Flag: Boolean;
str: string;
P: DWORD;
begin
i := 0;
Result := False;
ComboBox1.Clear;
handle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if handle <> 0 then
begin
Lpp.dwSize := SizeOf(Lpp); //这时在是关键 没有则falg 一直为false
flag := Process32First(handle, Lpp);
while flag do
begin
str := StrPas(Lpp.szExeFile); //这个不能少 否则得到的是空值
if Lpp.th32ProcessID <> 0 then
P := Lpp.th32ProcessID
else p := 0;

ComboBox1.AddItem(str, Pointer(p));
List[i, 0] := Lpp.th32ProcessID;
List[i, 1] := i;
inc(i);
Result := True;
Flag := Process32Next(handle, Lpp);
end;
end;
ComboBox1.ItemIndex := 0;
CloseHandle(handle);
end;



procedure TForm1.AddToCxgrid(itemindex: Integer; Flag: integer);
var
ListBox: TStringList;
filename, filePath: string;
n: Integer; //从n行开始添加数据
m: Integer; //list里的行数
number: Integer;
function GetAllList(ItemIndex: Integer): TStringList;
var
ProcessID: DWORD;
handle : Thandle;
flag : Boolean;
hmod: tagMODULEENTRY32W;
FprocessEntry32: TProcessEntry32;
P: DWORD;
i, j: Integer;
begin
Result := TStringList.Create;
ProcessID := GetProcessId(Cardinal(List[ItemIndex, 0])); //这时一定要转换 不能直接用Cardinal(List[ItemIndex, 0])
handle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessID);
if handle <> 0 then
begin
hmod.dwSize := SizeOf(hmod);
flag := Module32First(handle, hmod);
while flag do
begin
if hmod.th32ModuleID <> 0 then
P := hmod.th32ModuleID
else p := 0;
// Memo1.Lines.Add(hmod.szExePath);
Result.Add( Trim(hmod.szExePath));
flag := Module32Next(handle, hmod);
end;
end;
CloseHandle(handle);
end;
begin
m := 0;
number := 0;
filename := '';
if (itemindex < 0) or (itemindex > ComboBox1.Items.Count)then
Exit;

Listbox := TStringList.Create;
Listbox := getalllist(itemindex); //对就返回每一个进程的所有的dll
if ListBox.Count = 0 then
Exit;

n := cxGrid1DBTableView1.DataController.RecordCount; //取得cxgrid里现有的总行数
filename := ComboBox1.Items[itemindex];
Memo1.Lines.Add(filename + ' ' + IntToStr(itemindex));
cxGrid1DBTableView1.DataController.RecordCount := cxGrid1DBTableView1.DataController.RecordCount
+ ListBox.Count;
number := 0;
if flag = 1 then
begin
for m := n to ListBox.Count + n -1 do
begin
cxGrid1DBTableView1.DataController.Values[m, 0] := filename;
Memo2.Lines.Add(filename + ' ' + IntToStr(itemindex));
cxGrid1DBTableView1.DataController.Values[m, 1] := ListBox.Strings[number];
Inc(number);
end;
end
else if flag = 2 then
begin
for m := n to ListBox.Count -1 do
begin
cxGrid1DBTableView1.DataController.Values[m, 0] := filename;
Memo2.Lines.Add(filename + ' ' + IntToStr(itemindex));
cxGrid1DBTableView1.DataController.Values[m, 1] := ListBox.Strings[number];
Inc(number);
end;
end;
ListBox.Free;
end;

 


posted on 2012-03-22 16:17  long6  阅读(527)  评论(0编辑  收藏  举报

导航