delphi 如何得到外部exe文件进程句柄

来源:百度知道 编辑:UC知道 时间:2024/06/06 17:31:06

我给你个例子吧.
implementation
uses
TlHelp32;
{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
h:Cardinal;
pe:PROCESSENTRY32;
b:Boolean;
aItem:TListItem;
begin
lv1.Clear;
h:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);//遍历进程
if h = INVALID_HANDLE_VALUE then //failed
Exit;
ZeroMemory(@pe,SizeOf(pe));//initial
pe.dwSize:=SizeOf(pe);//important
b:=Process32First(h,pe);
while b do
begin
aItem:=lv1.Items.Add;
aItem.Caption:=IntToStr(pe.th32ProcessID);//PID,如果要得到进程句柄用OpenProcess,参数就有这个PID
aItem.SubItems.Add(pe.szExeFile);//进程的exe,要判断进程就只有根据这个值了
b:=Process32Next(h,pe)
end;
end;

你只要在这里加上判读exe是不是你所指的外部exe,如果是就用OpenProcess吧,然后break

yourhandle:=FindWindow('你这个exe的类型', '你这个exe的窗体名字');
两个填一个就可以
不知道的用nil代替

先调用CreateToolhelp32Snapshot形成一个快照,然后调用Process32First