Delphi searches for and ends Processes

Source: Internet
Author: User
Function tfrmimportexcel. findprocess (afilename: string): Boolean;
Hsnapshot: thandle; // used to obtain the Process List
Lppe: tprocessentry32; // used to find a process
Found: Boolean; // used to determine whether process traversal is complete
Killhandle: thandle; // used to kill a process
Result: = false;
Hsnapshot: = createconlhelp32snapshot (th32cs_snapprocess, 0); // obtain the list of System Processes
Lppe. dwsize: = sizeof (tprocessentry32); // You must initialize the lppe record size before calling the process32first API.
Found: = process32first (hsnapshot, lppe); // read the first process information in the process list into the PPE record.
While found do
If (uppercase (extractfilename (lppe. szexefile) = uppercase (afilename) or (uppercase (lppe. szexefile) = uppercase (afilename) then
{If msshow ('Do I close the Excel file if I find it opened? ', 2) = 6 then
// Since my operating system is XP, before calling the terminateprocess API
// I must first get the permission to shut down the process. If the operating system is below nt, you can directly stop the process.
Killhandle: = OpenProcess (process_terminate, false, lppe. th32processid );
Terminateprocess (killhandle, 0); // force process Shutdown
Closehandle (killhandle );
End ;}
Result: = true;
Found: = process32next (hsnapshot, lppe); // read the information of the next process in the Process List into the lppe record.
End; Procedure tfrmimportexcel. endprocess (afilename: string );
Process_terminate =$ 0001;
Continueloop: bool;
Fsnapshothandle: thandle;
Fprocessentry32: tprocessentry32;
Fsnapshothandle: = createconlhelp32snapshot (th32cs_snapprocess, 0 );
Fprocessentry32.dwsize: = sizeof (fprocessentry32 );
Continueloop: = process32first (fsnapshothandle, fprocessentry32 );
While INTEGER (continueloop) <> 0 do
If (uppercase (extractfilename (fprocessentry32.szexefile) = uppercase (afilename) or (uppercase (fprocessentry32.szexefile) = uppercase (afilename) then
Terminateprocess (OpenProcess (process_terminate, bool (0), fprocessentry32.th32processid), 0 );
Continueloop: = process32next (fsnapshothandle, fprocessentry32 );
End; // call if findprocess ('excel. EXE ') then
If msshow ('indicates that an Excel file has been opened, do you want to disable it? ', 2) <> 6 then exit;
Endprocess ('excel. EXE ');

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: 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.