delphi中关闭进程。代码如下
需要引用单元 TlHelp32
function KillTask(ExeFileName: string): boolean;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOLean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
res:Integer;
begin
Result :=false;
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
res := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
if res<>0 then Result :=True;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);//当前进程列表中所有的快照
end;
CloseHandle(FSnapshotHandle);
end;