Two ways of Delphi kill process

Source: Internet
Author: User

The first kind: relatively simple, according to the title, find the window, then find the process, kill the process

[Delphi]View PlainCopy
  1. Procedure Killprogram (windowtitle: string);
  2. Const
  3. Process_terminate = $0001;
  4. Var
  5. Processhandle:thandle;
  6. Processid:integer;
  7. Thewindow:hwnd;
  8. Begin
  9. Thewindow: = FindWindow (nil, PChar (WindowTitle));
  10. GetWindowThreadProcessId (Thewindow, @ProcessID);
  11. ProcessHandle: = OpenProcess (Process_terminate, FALSE, ProcessId);
  12. TerminateProcess (ProcessHandle,4);
  13. End


The second: A little more complicated, but can get more information, other functions of the time can refer to

[Delphi]View PlainCopy
  1. function Killtask (exefilename: string): integer;
  2. Const
  3. process_terminate=$0001;
  4. Var
  5. Continueloop:bool;
  6. Fsnapshothandle:thandle;
  7. fprocessentry32:tprocessentry32;
  8. Begin
  9. Result: = 0;
  10. Fsnapshothandle: = CreateToolhelp32Snapshot
  11. (Th32cs_snapprocess, 0);
  12. FProcessEntry32. dwsize: = Sizeof (FPROCESSENTRY32);
  13. Continueloop: = Process32First (Fsnapshothandle,
  14. FPROCESSENTRY32);
  15. while integer (continueloop) <> 0 do
  16. begin
  17. if (Uppercase (Extractfilename (FProcessEntry32. szexefile)) =
  18. Uppercase (Exefilename))
  19. or (Uppercase (FProcessEntry32. Szexefile) =
  20. Uppercase (Exefilename)) then
  21. Result: = Integer (TerminateProcess (OpenProcess (
  22. Process_terminate, BOOL (0),
  23. FProcessEntry32. Th32processid), 0));
  24. Continueloop: = Process32Next (Fsnapshothandle,
  25. FPROCESSENTRY32);
  26. end;
  27. CloseHandle (Fsnapshothandle);
  28. End

Also, Word can end the process according to the title, the rule of Word title is application title + "-" + window title

http://blog.csdn.net/youthon/article/details/7247874

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.