Two ways of Delphi kill process
UInt
Unit TLHELP32;
The first kind: relatively simple, according to the title, find the window, then find the process, kill the process
Procedure Killprogram (windowtitle:string);
Const
Process_terminate = $0001;
Var
Processhandle:thandle;
Processid:integer;
Thewindow:hwnd;
Begin
Thewindow: = FindWindow (Nil, PChar (WindowTitle));
GetWindowThreadProcessId (Thewindow, @ProcessID);
ProcessHandle: = OpenProcess (Process_terminate, FALSE, ProcessId);
TerminateProcess (processhandle,4);
End
The second: A little more complicated, but can get more information, other functions of the time can refer to
function Killtask (exefilename:string): integer;
Const
process_terminate=$0001;
Var
Continueloop:bool;
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
Two ways of Delphi kill process