delphi获取进程和模块信息


本文整理自网络,侵删。

 unit Unit1; 

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, TLHelp32;
type

TForm1 = class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
ListBox1: TListBox;
ListBox2: TListBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FSnapshotHandle: THandle;
ModuleArray: array of TModuleEntry32;
function GetProcessID(var List: TStringList; FileName: string = ''): TProcessEntry32;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}


function Tform1.GetProcessID(var List: TStringList; FileName: string = ''): TProcessEntry32;
var
Ret: BOOL;
s: string;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
Ret := Process32First(FSnapshotHandle, FProcessEntry32);
while Ret do
begin
s := ExtractFileName(FProcessEntry32.szExeFile);
if (FileName = '') then
List.Add(PChar(s))
else if (AnsicompareText(Trim(s),Trim(FileName))=0) and (FileName <> '') then
begin
List.Add(Pchar(s));
result := FProcessEntry32;
break;
end;
Ret := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
FProcessEntry32: TProcessEntry32;
PID: integer;
List: TStringList;
ModuleListHandle: Thandle;
ModuleStruct: TMODULEENTRY32;
J: integer;
Yn: boolean;
begin
if Combobox1.itemindex = -1 then exit;
List := TStringList.Create;
FProcessEntry32 := GetProcessID(List, Combobox1.text);
PID := FProcessEntry32.th32ProcessID;
ModuleListHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pID);
ListBox1.Items.Clear;
ModuleStruct.dwSize := sizeof(ModuleStruct);
yn := Module32First(ModuleListHandle, ModuleStruct);
j := 0;
while (yn) do
begin
SetLength(ModuleArray, j + 1);
ModuleArray[j] := ModuleStruct;
ListBox1.Items.Add(ModuleStruct.szExePath);
yn := Module32Next(ModuleListHandle, ModuleStruct);
J := j + 1;
end;
CloseHandle(ModuleListHandle);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
List: TStringList;
i: integer;
begin
Combobox1.clear;
List := TStringList.Create;
GetProcessID(List);
for i := 0 to List.Count - 1 do
begin
Combobox1.items.add(Trim(List.strings[i]));
end;
List.Free;
Combobox1.itemindex := 0;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
I: integer;
begin
Listbox2.Clear;
if Listbox1.itemindex = -1 then exit;
for i := 0 to Length(ModuleArray) do
begin
if UpperCase(Listbox1.items[Listbox1.itemindex]) = UpperCase(ModuleArray[i].szExePath) then
begin
Listbox2.Items.add('模块名称:' + ModuleArray[i].szModule);
Listbox2.items.add('模块ID:' + IntToStr(ModuleArray[i].th32ModuleID));
Listbox2.items.add('所属进程ID:' + IntToStr(ModuleArray[i].th32ProcessID));
Listbox2.Items.add('全局使用数:' + intToStr(ModuleArray[i].GlblcntUsage));
Listbox2.items.add('进程使用数:' + IntToStr(ModuleArray[i].ProccntUsage));
ListBox2.items.add(format('模块基地址:%.8X' ,[Integer(ModuleArray[i].modBaseAddr)]));
Listbox2.items.add(format('模块大小:%.8X' ,[ModuleArray[i].modBaseSize]));
Listbox2.items.add(format('模块句柄:%.8X' ,[ModuleArray[i].hModule]));
exit;
end;
end;
end;

end.

相关阅读 >>

Delphi idhttpserver实现webservice

Delphi settextbuf

Delphi 内存管理[2]

Delphi中dataset和json的互转

Delphi form的borderstyle属性

Delphi 自带的 base64 编解码函数

Delphi 设置系统静音取消系统静音

Delphi 检测程序有没有被dll注入的代码

Delphi xe10 手机程序事件服务操作、退出键操作

Delphi adoconnection1连接mssql数据库方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...