function GetProcModuleID(ProcessID:DWORD):DWORD;
var b:BOOL;
SnapHandle:THandle;
Entry:TProcessEntry32;
begin
SnapHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
Entry.dwSize:=Sizeof(Entry);
b:=Process32First(SnapHandle,Entry);
while integer(b)<>0 do begin
if Entry.th32ProcessID=ProcessID then Result:=Entry.th32ModuleID;
b:=Process32Next(SnapHandle,Entry);
end;
end;
function GetProcSize(ProcessID:DWORD):DWORD;
var b:BOOL;
SnapHandle:THandle;
Entry:TModuleEntry32;
ModuleID:DWORD;
begin
ModuleID:=GetProcModuleID(ProcessID);
SnapHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPALL,ProcessID);
Entry.dwSize:=Sizeof(Entry);
b:=Module32First(SnapHandle,Entry);
while integer(b)<>0 do begin
if Entry.th32ModuleID=ModuleID then Result:=Entry.modBaseSize;
b:=Module32Next(SnapHandle,Entry);
end;
end;
procedure TMainFrm.btnListClick(Sender: TObject);
var
p: pProcessInfo;
ContinueLoop: BOOL;
FSnapshotHandle, hProcess: THandle;
FProcessEntry32: TProcessEntry32;
n: integer;
b:BOOL;
SnapHandle:THandle;
Entry:TModuleEntry32;
begin
FSL.Clear;
n:=1;
New(p);
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
p.ExeFile:= FProcessEntry32.szExeFile;
///////////////////////
b := Module32First(FSnapshotHandle, Entry);
while integer(b)<>0 do begin
if Entry.th32ModuleID=FProcessEntry32.th32ProcessID then
break;
b:=Module32Next(FSnapshotHandle,Entry);
end;
//
ListBox1.Items.Add(inttostr(n)+':'+inttostr
(FProcessEntry32. th32ProcessID)+' * ' +
p.ExeFile + ' 内存:' + IntToStr(GetProcSize(FProcessEntry32.th32ProcessID)));//Entry.modBaseSize));
inc(n);
FSL.Add(p.ExeFile);
if UpperCase(p.ExeFile)='CALC.EXE' then
begin
hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE,
FProcessEntry32.th32ProcessID);
TerminateProcess(hProcess,0);
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
procedure TMainFrm.KillProc(ProcName: string);
var
p: pProcessInfo;
ContinueLoop: BOOL;
FSnapshotHandle, hProcess: THandle;
FProcessEntry32: TProcessEntry32;
begin
if trim(ProcName) = '' then exit;
ProcName := UpperCase(ProcName);
ProcName := ExtractFileName(ProcName);
if Pos('.EXE', ProcName) < 1 then ProcName := ProcName + '.EXE';
New(p);
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
p.ExeFile:= FProcessEntry32.szExeFile;
//if UpperCase(ExtractFileName(p.ExeFile)) = ProcName then
if Pos(UpperCase(ExtractFileName(p.ExeFile)), ProcName) > 0 then
begin
hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE,
FProcessEntry32.th32ProcessID);
TerminateProcess(hProcess,0);
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
procedure TMainFrm.Button1Click(Sender: TObject);
const
ExceptPro = 'EXPLORE.EXE,DELPHI32.EXE,TOOLS.EXE,QQ.EXE,KILLPROC.EXE'; //我这里只过滤了五个,其他要过滤加入就行了,但必需要大小
var
lp: integer;
PorceName: string;
begin
for lp := 0 to FSL.Count - 1 do
begin
PorceName := UpperCase(trim(ExtractFileName(FSl[lp])));
if Pos(PorceName, ExceptPro) > 0 then Continue; //在这里过滤不想Kill进程
if trim(PorceName) <> '' then
KillProc(PorceName);
end;
end;
procedure TMainFrm.FormCreate(Sender: TObject);
begin
FSL := TStringList.Create;
end;
procedure TMainFrm.FormDestroy(Sender: TObject);
begin
FSL.Free;
end;