procedure TF_Update.KillProcess(AProcName: string);//AProcName为进程的名称
var
lppe: TProcessEntry32;
found: boolean;
Hand: THandle;
begin
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
found := Process32First(Hand, lppe);
while found do
begin
if UpperCase(AProcName) = UpperCase(lppe.szExeFile) then
begin
TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, lppe.th32ProcessID), 0);
found := false;
end
else
found := Process32Next(Hand, lppe);
end;
end;
function MyFun_GetProcessID(ProcessName: string): Cardinal;
var
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
Ret: Boolean;
TmpProcessName: string;
TmpProcessID: DWORD;
begin
TmpProcessID := 0; //如果没有找到
//创建系统快照
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
//先初始化 FProcessEntry32 的大小
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
Ret := Process32First(FSnapshotHandle, FProcessEntry32);
//循环枚举出系统开启的所有进程,找出“ProcessName”所指定的进程
while Ret do
begin
TmpProcessName := ExtractFileName(FProcessEntry32.szExeFile);
if TmpProcessName = ProcessName then
begin
TmpProcessID := FProcessEntry32.th32ProcessID;
Break;
end;
Ret := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
Result := TmpProcessID;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
ProcessID: THandle;
FHandle: THandle;
begin
ProcessID := MyFun_GetProcessID('你的应用程序的文件名');
FHandle := OpenProcess(PROCESS_ALL_ACCESS,False,ProcessID);
TerminateProcess(FHandle,0);
end;
杀死进程的函数,传个exe文件在进程里显示的名字就行了
function KillTask(ExeFileName:string):integer;
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;