Delphi写进程管理器


本文整理自网络,侵删。

 unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Tlhelp32, ExtCtrls, Menus, psapi;

type
TForm1 = class(TForm)
Button1: TButton;
ListView1: TListView;
Label1: TLabel;
Button2: TButton;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure N2Click(Sender: TObject);
Private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
const
PROCESS_TERMINATE = $0001;
implementation

function GetPathFileofModule(ModuleName:String):String; //枚举进程所调用的模块及进程文件所在路径
var
hProcSnap: THandle;
pProcess: THandle;
pe32: TProcessEntry32;
buf:array[0..MAX_PATH] of char;
hMod:HMODULE;
cbNeeded:DWORD;
begin
hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
if hProcSnap = INVALID_HANDLE_VALUE then Exit;
pe32.dwSize := SizeOf(ProcessEntry32);
if Process32First(hProcSnap, pe32) = True then
while Process32Next(hProcSnap, pe32) = True do
begin
if uppercase(pe32.szExeFile)=uppercase(ModuleName) then
begin
pProcess:=OpenProcess(PROCESS_QUERY_INFORMATION or
PROCESS_VM_READ,
FALSE,
pe32.th32ProcessID);
if pProcess<>0 then
begin
if EnumProcessModules( pProcess,@hMod,sizeof(hMod),cbNeeded) then
begin
ZeroMemory(@buf,MAX_PATH+1);
GetModuleFileNameEx(pProcess, hMod,buf,MAX_PATH+1);
Result:=strpas(buf);
end;
end;
end;
end;
CloseHandle(hProcSnap);
end;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
found:boolean; //定义枚举进程所需变量
NewItem: TListItem;
FSnapshotHandle:tHANDLE;
lppe:TProcessEntry32;
Summ: Word;
begin
with listview1 do
begin
ViewStyle:=vsreport;
GridLines:=true;
columns.items[0].caption:='进程名';
columns.items[1].caption:='进程序号';
columns.items[2].caption:='进程ID';
columns.items[3].caption:='进程线程数';
columns.items[4].caption:='父进程ID';
columns.items[5].caption:='进程优先级';
columns.items[6].caption:='进程模块ID';
columns.items[7].caption:='进程默认堆栈';
columns.items[8].caption:='进程文件路径';
Columns.Items[0].Width:=100;
Columns.Items[1].Width:=60;
Columns.Items[2].Width:=60;
Columns.Items[3].Width:=75; //初始化listview
Columns.Items[4].Width:=60;
Columns.Items[5].Width:=75;
Columns.Items[6].Width:=75; //初始化listview
Columns.Items[7].Width:=85;
Columns.Items[8].Width:=300;

end;

//ListView1.Items.BeginUpdate;
ListView1.Items.Clear;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //CreateToolhelp32Snapshot函数得到进程快照

lppe.dwSize := Sizeof(lppe); //初始化
found := Process32First(FSnapshotHandle, lppe); //Process32First 得到一个系统快照里第一个进程的信息
Summ := 0;
while found do
begin
Summ := Summ + 1;
NewItem := ListView1.Items.Add; //在ListView1显示
//NewItem.ImageIndex := -1;
NewItem.Caption := ExtractFileName(lppe.szExeFile);//返回进程名称
NewItem.subItems.Add(FormatFloat('00', Summ));//返回序号
NewItem.subItems.Add(IntToStr(lppe.th32ProcessID));//返回进程ID
NewItem.SubItems.Add((IntToStr(lppe.cntThreads))); //返回进程线程数
NewItem.SubItems.Add((IntToStr(lppe.th32ParentProcessID))); //返回父进程ID
NewItem.SubItems.Add((IntToStr(lppe.pcPriClassBase))); //返回线程优先权
NewItem.SubItems.Add((IntToStr(lppe.th32ModuleID))); //返回进程模块ID
NewItem.SubItems.Add((IntToStr(lppe.th32DefaultHeapID))); //返回进程默认堆栈
NewItem.SubItems.Add(GetPathFileofModule(ExtractFileName(lppe.szExeFile))); //返回每个进程文件所在路径
found := Process32Next(FSnapshotHandle, lppe);
end;
CloseHandle(FSnapshotHandle);
//ListView1.Items.EndUpdate;
self.Label1.Caption:='当前系统共有'+''+inttostr(listview1.Items.count)+''+'个进程' ;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
hProcess:Thandle;
Ret:Boolean;
begin
hProcess:=OpenProcess(PROCESS_ALL_ACCESS, False,StrToInt(ListView1.Selected.SubItems.Strings[1]));
Ret:=Terminateprocess(hProcess,0);
if Ord(Ret)<>0 then
begin
ListView1.DeleteSelected;
//Self.FormCreate(Sender); //如果成功结束选中进程则自动刷新进程列表
Label1.Caption:='当前系统共有'+''+IntToStr(Listview1.Items.count)+''+'个进程' ;
//MessageBox(Handle, PChar('该进程已被成功结束!'), PChar('提示'), MB_OK);
CloseHandle(hProcess);
end
else
begin
MessageBox(Handle, PChar('进程' + Listview1.Selected.caption +'不能被结束!'), PChar('提示'), MB_ICONEXCLAMATION);
end
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
listview1.Items.Clear;
self.FormCreate(Sender);
//ShowMessage(ListView1.Selected.SubItems.Strings[1]); //返回选中行第三列中的值
end;

procedure TForm1.N2Click(Sender: TObject);
var
hProcess:hwnd;
begin
hProcess:=OpenProcess(PROCESS_ALL_ACCESS, False,StrToInt(ListView1.Selected.SubItems.Strings[1]));
showwindow(hProcess,SW_SHOW);
end;
end.

相关阅读 >>

Delphi webbrowser1 设置获取编码

Delphi图片增加文字水印

Delphi 去掉文件只读属性

Delphi xe7 取得进程占用内存的两个函数

Delphi 串口 字符串转换ascii

Delphi中建议使用的语句

Delphi is 与 as 运算符举例

Delphi 判断文本文件的编码格式

Delphi接收文件拖放

Delphi 判断一个数组的长度用 length 还是 sizeof ?

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



打赏

取消

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

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

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

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

评论

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