1,184
社区成员
发帖
与我相关
我的任务
分享
uses
TLHelp32;
//根据应用名字结束指定模块路径的程序,结束所有
//ModuleFullPath --程序路径,如: c:\test\test.exe
//AppName --程序名, 如:test.exe,可省略不写
function KillProcessByFullPath(ModuleFullPath: string; AppName: string = ''): integer;
var
lppe: TPROCESSENTRY32;
found: boolean;
ProcessList: THandle;
ModuleList: Thandle;
pm: TMODULEENTRY32;
h: Thandle;
a: DWORD;
ModuleName: string;
AppProcessId: DWORD; // 线程ID
begin
Result := 0;
AppProcessId := GetCurrentProcessId;
if Length(AppName) = 0 then
AppName := ExtractFileName(ModuleFullPath);
lppe.dwSize := sizeof(TPROCESSENTRY32);
pm.dwSize := sizeof(TMODULEENTRY32);
ProcessList := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
found := Process32First(ProcessList, lppe);
while found do //进程列表
begin
if StrIComp(PChar(StrPas(lppe.szExeFile)), PChar(AppName)) = 0 then //应用程序名字是否相同?
begin
ModuleList := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, lppe.th32ProcessID);
found := module32first(ModuleList, pm);
while found do //模块列表
begin
ModuleName := StrPas(pm.szexepath);
ModuleName := copy(ModuleName, max(1, pos(':', ModuleName) - 1), MaxInt);
if StrIComp(pchar(ModuleName), pchar(ModuleFullPath)) = 0 then //是否包含要找的模块
begin
if AppProcessId <> lppe.th32ProcessID then
begin
h := openprocess(PROCESS_ALL_ACCESS, TRUE, lppe.th32ProcessID);
GetExitCodeProcess(h, a);
if Integer(TerminateProcess(h, a)) <> 0 then
inc(Result);
end;
break;
end;
found := module32next(ModuleList, pm);
end;
closehandle(ModuleList);
end;
found := Process32Next(ProcessList, lppe);
end;
closehandle(ProcessList);
end;