怎样关闭外部程序?50分(50分)

  • 主题发起人 主题发起人 iseek
  • 开始时间 开始时间
I

iseek

Unregistered / Unconfirmed
GUEST, unregistred user!
如果我知道一外部程序的名称和路径,但我不知道它的类名和<br>caption,我如何编程关闭它?<br>——iseek
 
杀除进程不就可以了。
 
有两种方法:<br>procedure TForm1.CloseClient;<br>var<br>&nbsp; hCurrentWindow:HWnd;S:string;<br>&nbsp; szText:array[0..254] of char;<br>&nbsp; pCname:array[0..100] of char;<br>begin<br>&nbsp; &nbsp;hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);<br>&nbsp; &nbsp;while hCurrentWindow &lt;&gt; 0 do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;if GetWindowText(hCurrentWindow, @szText, 255) &gt; 0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;S:=StrPas(@szText);<br>&nbsp; &nbsp; &nbsp; &nbsp;if (pos('监控',s)&gt;0) and (pos('AGENT',uppercase(s))=0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if isWindow(hCurrentWindow) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetClassName(hCurrentWindow,pCname,100);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Not (Pos('CABINETWCLASS',UpperCase(StrPas(pCname)))&gt;0) then //检查是否为Forlder;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(hCurrentWindow,WM_CLOSE,0,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isClientRuned:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);<br>&nbsp; &nbsp;end;<br>end;<br>{var user tlhelp32;<br>&nbsp;lppe:tprocessentry32;sshandle:thandle;<br>&nbsp;hh:hwnd;found:boolean;<br>begin<br>sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);<br>found:=process32first(sshandle,lppe);<br>&nbsp;while found do<br>&nbsp;begin<br>&nbsp; //进行你的处理其中lppe.szExefile就是程序名。<br>&nbsp; &nbsp;if uppercase(extractfilename(lppe.szExeFile))='CLIENT.EXE' then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);<br>&nbsp; &nbsp; &nbsp;TerminateProcess(hh,0);<br>&nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp;isClientRuned:=True;<br>&nbsp; &nbsp; &nbsp;CloseHandle(sshandle);<br>&nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;found:=process32next(sshandle,lppe);<br>&nbsp;end;<br>&nbsp;CloseHandle(sshandle);<br>end; &nbsp;}<br>
 
关闭‘计算器’的例子。<br>var <br>HWndCalculator : HWnd; <br>begin <br>// find the exist calculator window <br>HWndCalculator := Winprocs.FindWindow(nil, '计算器'); // close the exist Calculator <br>if HWndCalculator &lt;&gt; 0 then <br>SendMessage(HWndCalculator, WM_CLOSE, 0, 0); <br>end;
 
玩点狠的terminateprocess
 
DragonPC_???说的对,<br>先得到进程的ProcessID,然后由ProcessID得到ProcessHandle<br>OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE, FALSE,<br>&nbsp; &nbsp; &nbsp; &nbsp; ProcessID)<br><br>TerminateProcess(ProcessHandle, 0);
 
就这样吧,谢谢各位
 
Aiirii写的Toolhelp32函数仅仅适用于 Win9x<br>WinNT/2000 需要使用PSAPI函数来判断文件名获取ProcessHandle,<br>然后再使用TerminateProcess杀进程,基本原理和Aiirii一样的。<br>
 
后退
顶部