怎样关闭一个无标题的应用程序(200分)

  • 主题发起人 主题发起人 3DDELPHI
  • 开始时间 开始时间
3

3DDELPHI

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好!<br>最近我遇到一个问题:我想通过一个Timer控件隔一段时间去检测我想要关闭的应用程序,<br>对有标题的程序(如:豪杰超级解霸2001)没什么问题,<br>先用HWND:=Findwindow(nil,'豪杰超级解霸2001'),<br>然后用PostMessage(hwnd,wm_close,0,0)发送一个消息就行了。<br>但对没有标题的程序(如:金山影霸Ⅲ共享版),它只有一个窗口,<br>根本没有findwindow()的第二个参数,用什么法可以关闭它,谢谢!
 
如果你知道这个窗口的类名,可以用它作为第一个参数,第二个参数为空
 
用Delphi带的Winsight或者VC的Spy++ 可以获得该程序的类名。
 
同意上面两位
 
用CreateProcess打开进程,可得到句柄和ID,用TerminateProcess杀之。
 
to probor;呵呵,我是个新手,能告说说什么叫类名吗?
 
你的Form中不是有TForm1吗?TForm1就是类名。当然,每个程序的窗体得类名不一样的。<br>用Spy++可以查看到
 
知道程序名也可关闭它。<br>下面两段代码希望对你有用:<br><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 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//检查是否为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; file://进行你的处理其中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>uses tlhelp32;<br>假设要终止的程序的文件名为:project2.exe,那么例程如下:<br>var<br>lppe:tprocessentry32;<br>sshandle:thandle;<br>hh:hwnd;<br>found:boolean;<br>begin<br>sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);<br>found:=process32first(sshandle,lppe);<br>while found do<br>begin<br>&nbsp; file://进行你的处理其中lppe.szExefile就是程序名。<br>&nbsp; if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' then<br>&nbsp; begin<br>&nbsp; &nbsp; hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);<br>&nbsp; &nbsp; TerminateProcess(hh,0);<br>&nbsp; end;<br>&nbsp; found:=process32next(sshandle,lppe);<br>end;<br>end;<br>
 
多人接受答案了。
 
后退
顶部