var pe:PROCESSENTRY32; me:MODULEENTRY32; hp,hm:Thandle; b,b1:boolean; begin hp:=CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0); pe.dwSize:=SizeOf(pe); b:=Process32First(hp,pe); while b do begin hm:=CreateToolHelp32SnapShot(TH32CS_SNAPModule,pe.th32ProcessID); me.dwSize:=sizeof(ModuleEntry32); if Module32First(hm,me) then begin b1:=Module32First(hm,me); while b1 do begin // if me.th32ModuleID=pe.th32ModuleID then listbox1.Items.Add(copy(me.szExePath,length(extractfilepath(me.szExePath))+1,length(me.szExePath)-length(extractfilepath(me.szExePath)))); //移到路径只要文件名; b1:=Module32Next(hm,me); end; end; b:=Process32Next(hp,pe); end;这段代码可以列出当前所有在运行的EXE文件和DLL文件; 你在LISTBOX1中查找是否有你想要查找的文件(如DELPHI32.EXE),用一个循环就可搞定; for i:=0 to listbox1.items.count-1 do begin if listbox1.items[i]='delphi32.exe' then begin 存在,执行存在代码; break; end else 不存在; end;
pe:PROCESSENTRY32;
me:MODULEENTRY32;
hp,hm:Thandle;
b,b1:boolean;
begin
hp:=CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
pe.dwSize:=SizeOf(pe);
b:=Process32First(hp,pe);
while b do
begin
hm:=CreateToolHelp32SnapShot(TH32CS_SNAPModule,pe.th32ProcessID);
me.dwSize:=sizeof(ModuleEntry32);
if Module32First(hm,me) then
begin
b1:=Module32First(hm,me);
while b1 do
begin
// if me.th32ModuleID=pe.th32ModuleID then
listbox1.Items.Add(copy(me.szExePath,length(extractfilepath(me.szExePath))+1,length(me.szExePath)-length(extractfilepath(me.szExePath)))); //移到路径只要文件名;
b1:=Module32Next(hm,me);
end;
end;
b:=Process32Next(hp,pe);
end;这段代码可以列出当前所有在运行的EXE文件和DLL文件;
你在LISTBOX1中查找是否有你想要查找的文件(如DELPHI32.EXE),用一个循环就可搞定;
for i:=0 to listbox1.items.count-1 do
begin
if listbox1.items[i]='delphi32.exe' then
begin
存在,执行存在代码;
break;
end
else
不存在;
end;
2、或者用WinApi函数寻找当前运行的程序中是否有目标应用程序,FindWindows应该可以,如何使用参看Delphi帮助或者MSDN。
Windows, Messages, SysUtils, Classes, Graphics, TLHelp32, Controls, Forms, Dialogs,
StdCtrls;type
TProcessInfo = record
ExeFile: string;
ProcessId: DWORD;
end;
ProcessInfo = ^TProcessInfo;
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ProcessList(var pList: TList);
procedure My_RunFileScan(ListboxRunFile: TListBox);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
Current: TList;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ProcessList(var pList: TList);
var
p: ProcessInfo;
ok: Bool;
ProcessListHandle: THandle;
ProcessStruct: TProcessEntry32;
begin
PList := TList.Create;
PList.Clear;
ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessStruct.dwSize := Sizeof(ProcessStruct);
ok := Process32First(ProcessListHandle, ProcessStruct);
while Integer(ok) <> 0 do
begin
new(p);
p.ExeFile := ProcessStruct.szExeFile;
p.ProcessID := ProcessStruct.th32ProcessID;
PList.Add(p);
ok := Process32Next(ProcessListHandle, ProcessStruct);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
h: THandle;
a: DWORD;
p: PRocessInfo;
begin
if ListBox1.ItemIndex >= 0 then
begin
p := Current.Items[ListBox1.ItemIndex];
h := openProcess(Process_All_Access, true, p.ProcessID);
GetExitCodeProcess(h, a); if Integer(TerminateProcess(h, a)) <> 0 then
begin
My_RunFileScan(ListBox1);
end;
end
else
Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK);
end;procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox);
var
i: Integer;
p: PRocessInfo;
begin
current := TList.Create;
Current.Clear;
ListboxRunFile.Clear;
ProcessList(Current);
for i := 0 to Current.Count - 1 do
begin
new(p);
p := Current.Items[i];
ListboxRunFile.Items.Add(p.ExeFile);
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
My_RunFileScan(ListBox1);
end;end.