1. 程式人生 > >delphi殺進程的兩種方式

delphi殺進程的兩種方式

ring targe app tails exe nap creat rmi 復雜

第一種:比較簡單,根據標題,找到窗口,再找到進程,殺死進程

[delphi] view plain copy
  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;


第二種:復雜一點,不過可獲取的信息更多,其他功能的時候可以參考一下

[delphi] view plain copy
  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;

另,word也可以根據標題結束進程,word的標題的規則是application的title + "-" + window的title

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

delphi殺進程的兩種方式