unit UnitMain;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs,tlhelp32, ExtCtrls, StdCtrls, shellapi;<br><br>type<br> TForm1 = class(TForm)<br> Timer1: TTimer;<br> Timer2: TTimer;<br> procedure Timer1Timer(Sender: TObject);<br> procedure Timer2Timer(Sender: TObject);<br> private<br>// procedure MyTimer(Sender:TObject);<br> function KillTask(ExeFileName: string): Integer;<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>procedure MyTimer(Sender:TObject);<br>begin<br> if Form1.KillTask('QQ.exe')> 0 then<br> showmessage('成功');<br>end;<br><br>function FindProcessName:THandle;<br>var<br> lppe: tprocessentry32;<br> sshandle: thandle;<br> found: boolean;<br>begin<br> result:=0;<br> sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);<br> found := process32first(sshandle, lppe);<br> while found do<br> begin<br> if ansiCompareText(ExtractFileName(lppe.szExefile),'QQ.exe') = 0 then<br> begin<br> result:=lppe.th32ProcessID;<br> break;<br> end;<br> found := process32next(sshandle, lppe); {检索下一个进程}<br> end;<br> CloseHandle(sshandle);<br>end;<br><br>function TForm1.KillTask(ExeFileName: string): Integer;<br>const<br> PROCESS_TERMINATE = $0001;<br>var<br> ContinueLoop: BOOL;<br> FSnapshotHandle: THandle;<br> FProcessEntry32: TProcessEntry32;<br>begin<br> Result := 0;<br> FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> FProcessEntry32.dwSize := SizeOf(FProcessEntry32);<br> ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);<br> while Integer(ContinueLoop) <> 0 do<br> begin<br> if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then<br> Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,BOOL(0),FProcessEntry32.th32ProcessID),0));<br> ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);<br> end;<br> CloseHandle(FSnapshotHandle);<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br> h: THandle;<br>begin<br> h:=FindProcessName;<br> if h<>0 then begin<br>// if KillTask('QQ.exe')> 0 then<br>// showmessage('成功');<br> Timer2.Enabled := True;<br>// SetTimer(0, 0, 120, @MyTimer);<br>// ShellExecute(0,'open','E:/BJGE/Program Files/Tencent/QQ.exe',nil,nil,sw_hide);<br>// winexec('E:/BJGE/Program Files/Tencent/QQ.exe',sw_normal);<br> end;<br>end;<br><br><br>procedure TForm1.Timer2Timer(Sender: TObject);<br>begin<br> if KillTask('QQ.exe')> 0 then<br> showmessage('成功');<br>end;<br><br>end.