
我的方法:
1。根据进程名获取进程ID
2。利用进程ID循环所有进程,找到相同ID的进程,利用TerminateProcess关闭进程。
代码如下:
var
hHandle, hProc: THandle;
PE32: TProcessEntry32;
bFind: Boolean;
begin
hHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PE32.dwSize := SizeOf(TProcessEntry32);
bFind := Process32First(hHandle, PE32);
while bFind do
begin
if (UpperCase(ATask) = UpperCase(PE32.szExeFile)) or
(UpperCase(ATask) = UpperCase(ExtractFileNam(PE32.szExeFile))) then
begin
hProc := OpenProcess(PROCESS_TERMINATE,False,PE32.th32ProcessID);
TerminateProcess(hProc, 0); //查找到则关闭
end;
bFind := Process32Next(hHandle, PE32);
end;
end;
uses
Tlhelp32;
function KillTask(ExeFileName:string):boolean;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOLean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
现成不用是傻蛋 ,直接说你笨 不会用 就是了。何必装呢
winexec('cmd.exe /c taskkill /f /t /im QQ.exe',sw_hide);