Two ways of Delphi kill process

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.