怎样通过程序关闭一个正在运行中的应用程序 (200分)

  • 主题发起人 主题发起人 jarm
  • 开始时间 开始时间
J

jarm

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; handle: integer;<br>begin<br>&nbsp; handle := findwindow(nil,'myproject');<br>&nbsp; PostMessage(handle,WM_CLOSE,0,0);//应用程序会弹出一个是与否的对话框<br>&nbsp; EnumChildWindows(handle,@EnumerateChildWindows,0)//这里得不到“是”按钮得句柄,<br>&nbsp; 因此没办法给“是”按钮发送消息。<br>end;<br><br>function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL;<br>var<br>&nbsp; &nbsp;ClassName: Array[0..255] of char; &nbsp;// this holds the class name of our child windows<br>begin<br>&nbsp; &nbsp;{get the class name of the given child window}<br>&nbsp; &nbsp;GetClassName(hWnd,ClassName,255);<br>&nbsp; &nbsp;ShowMessage(ClassName); <br>&nbsp; &nbsp;Result:=TRUE;<br>end;<br>
 
上面的代码怎么啦,关不了用这个。<br>procedure ChePortProcess;<br>var<br>&nbsp; hSnapshot: THandle;<br>&nbsp; ProcessEntry: TProcessEntry32;<br>&nbsp; hProcess: THandle;<br>&nbsp; sFileName: string;<br>&nbsp; iProcessID: Integer;<br>&nbsp; iOK:Boolean;<br>begin<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; &nbsp; &nbsp; ProcessEntry.dwSize := SizeOf(TProcessEntry32);<br>&nbsp; &nbsp; &nbsp; iOK := Process32First(hSnapshot, ProcessEntry);<br>&nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; sFileName := ExtractFileName(ProcessEntry.szExeFile); &nbsp; &nbsp;//進程使用的EXE<br>&nbsp; &nbsp; &nbsp; &nbsp; if sFileName='RavProxy.exe' then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //瑞星杀毒<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iProcessID:=ProcessEntry.th32ProcessID; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //進程號<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TerminateProcess(OpenProcess(PROCESS_TERMINATE,BOOL(0),iProcessID),0); &nbsp; //杀死该进程<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; until (not Process32Next(hSnapshot, ProcessEntry));<br>&nbsp; &nbsp; &nbsp; CloseHandle(hSnapshot);<br>&nbsp; except<br>&nbsp; end;<br>end;
 
cbdiy说的没错<br>不过上面的代码一定要引用tlhelp32单元
 
直接杀进程是可以,但我担心应用程序会有些东西没有正常释放而带来不好的影响,<br>而且这个应用程序是一个后台程序,24小时都在不停运行着,我只是写个程序去定时<br>关闭它罢了
 
&gt; &nbsp;EnumChildWindows(handle,@EnumerateChildWindows,0)//这里得不到“是”按钮得句柄,<br>“是”在另外一个窗体上,你要先FindWindow到那个对话框的Handle
 
uses <br>&nbsp; Tlhelp32; <br><br>function KillTask(ExeFileName: string): Integer; <br>const <br>&nbsp; PROCESS_TERMINATE = $0001; <br>var <br>&nbsp; ContinueLoop: BOOL; <br>&nbsp; FSnapshotHandle: THandle; <br>&nbsp; FProcessEntry32: TProcessEntry32; <br>begin <br>&nbsp; Result := 0; <br>&nbsp; FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); <br>&nbsp; FProcessEntry32.dwSize := SizeOf(FProcessEntry32); <br>&nbsp; ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); <br><br>&nbsp; while Integer(ContinueLoop) &lt;&gt; 0 do <br>&nbsp; begin <br>&nbsp; &nbsp; if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = <br>&nbsp; &nbsp; &nbsp; UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = <br>&nbsp; &nbsp; &nbsp; UpperCase(ExeFileName))) then <br>&nbsp; &nbsp; &nbsp; Result := Integer(TerminateProcess( <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OpenProcess(PROCESS_TERMINATE, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BOOL(0), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FProcessEntry32.th32ProcessID), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0)); <br>&nbsp; &nbsp; &nbsp;ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); <br>&nbsp; end; <br>&nbsp; CloseHandle(FSnapshotHandle); <br>end; <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>begin <br>&nbsp; KillTask('notepad.exe'); <br>end; <br><br>{ For Windows NT/2000/XP } <br><br>procedure KillProcess(hWindowHandle: HWND); <br>var <br>&nbsp; hprocessID: INTEGER; <br>&nbsp; processHandle: THandle; <br>&nbsp; DWResult: DWORD; <br>begin <br>&nbsp; SendMessageTimeout(hWindowHandle, WM_CLOSE, 0, 0, <br>&nbsp; &nbsp; SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000, DWResult); <br><br>&nbsp; if isWindow(hWindowHandle) then <br>&nbsp; begin <br>&nbsp; &nbsp; // PostMessage(hWindowHandle, WM_QUIT, 0, 0); <br><br>&nbsp; &nbsp; { Get the process identifier for the window} <br>&nbsp; &nbsp; GetWindowThreadProcessID(hWindowHandle, @hprocessID); <br>&nbsp; &nbsp; if hprocessID &lt;&gt; 0 then <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; { Get the process handle } <br>&nbsp; &nbsp; &nbsp; processHandle := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION, <br>&nbsp; &nbsp; &nbsp; &nbsp; False, hprocessID); <br>&nbsp; &nbsp; &nbsp; if processHandle &lt;&gt; 0 then <br>&nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp; { Terminate the process } <br>&nbsp; &nbsp; &nbsp; &nbsp; TerminateProcess(processHandle, 0); <br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(ProcessHandle); <br>&nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; end; <br>&nbsp; end; <br>end; <br><br>procedure TForm1.Button2Click(Sender: TObject); <br>begin <br>&nbsp; KillProcess(FindWindow('notepad',nil)); <br>end;
 
继续。。。。
 
有另一种方法可以强行退出<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; handle: integer;<br>begin<br>&nbsp; handle := findwindow(nil,'myproject');<br>&nbsp; PostMessage(handle,WM_QUIT,0,0);//直接退出,不知这样做会不会把应用程序里的东西<br>&nbsp; &nbsp; 正常释放<br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; h, hi: HWND;<br>&nbsp; tmBegin: TTime;<br>begin<br>&nbsp; hi:= 0;<br>&nbsp; h:= FindWindow('TMainForm', 'MyPrj');<br>&nbsp; //这样可以强行退出,但是非正常退出<br>&nbsp; //if h &gt; 0 then PostMessage(h, WM_QUIT, 0, 0);<br>&nbsp; if h &gt; 0 then PostMessage(h, WM_CLOSE, 0, 0)//告诉它要退出<br>&nbsp; else Exit;//没找到你的程序<br>&nbsp; tmBegin := TTime(Now);<br>&nbsp; while (hi &lt;= 0) and (StrToInt(FormatDateTime('s', TTime(Now) - tmBegin)) &lt;= 5) do<br>&nbsp; begin &nbsp;<br>&nbsp; &nbsp; //延时5秒钟,在这段时间内查找“是与否的对话框”<br>&nbsp; &nbsp; //'Notice'是对话框的标题<br>&nbsp; &nbsp; hi := FindWindow(PChar(GlobalAddAtom(MAKEINTATOM($00008002))), 'Notice');<br>&nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; end;<br><br>&nbsp; if hi &lt;= 0 then Exit;过了5秒钟还没找到“是与否的对话框”就退出<br>&nbsp; //查找“是”按钮,如果是别的形式的对话框,自己修改好了<br>&nbsp; hi := FindWindowEx(hi, 0, 'Button', '是(&amp;Y)');<br>&nbsp; //在“是”按钮上面点了一下<br>&nbsp; SendMessage(hi, BM_CLICK, 0, 0);<br>end;<br>
 
上面延时的原因:<br>你调用PostMessage后立即调用了EnumChildWindows,<br>而这个时候目标程序有可能还没有弹出这个对话框,当然找不到了<br>如果过了5秒钟还找不到的话,那一般是没戏了...
 
多人接受答案了。
 
后退
顶部