The first kind: relatively simple, according to the title, find the window, then find the process, kill the process
[Delphi]View PlainCopy
- 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
[Delphi]View PlainCopy
- 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
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