请问各位高手!!DELPHI里怎么样去关闭一支特定的WINDOWS进程啊?(50分)

  • 主题发起人 主题发起人 orlen
  • 开始时间 开始时间
O

orlen

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位高手!!DELPHI里怎么样去关闭一支特定的WINDOWS进程啊?
 
type<br>&nbsp; TProcessInfo=Record<br>&nbsp; &nbsp; ExeFile:String;<br>&nbsp; &nbsp; ProcessID:Dword;<br>end;<br>pProcessInfo=^TProcessInfo;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; p:pProcessInfo;<br>&nbsp; ContinueLoop:Bool;<br>&nbsp; FSnapshotHandle,hProcess:THandle;<br>&nbsp; FProcessEntry32:TProcessEntry32;<br>&nbsp; n:integer;<br>begin<br>&nbsp; ListBox1.Items.Clear;<br>&nbsp; n:=1;<br>&nbsp; New(p);<br>&nbsp; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);<br>&nbsp; ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);<br>&nbsp; while integer(ContinueLoop)&lt;&gt;0 do<br>&nbsp; begin<br>&nbsp; &nbsp; p.ExeFile:=FProcessEntry32.sZExeFile;<br>&nbsp; &nbsp; ListBox1.Items.Add(inttostr(n)+':'+inttostr(FProcessEntry32.th32ProcessID)+'*'+p.ExeFile);<br>&nbsp; &nbsp; inc(n);<br>&nbsp; &nbsp; if p.ExeFile='C:/WINDOWS/SYSTEM/RNAAPP.EXE' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; p.ProcessID:=FProcessEntry32.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; hprocess:=openprocess(process_all_access,True,p.ProcessID);<br>&nbsp; &nbsp; &nbsp; TerminateProcess(hprocess,0);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Continueloop:=process32next(fsnapshothandle,fprocessentry32);<br>&nbsp; end;<br>end;
 
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; //进行你的处理其中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>********************<br>HANDLE hProcess<br>Windows NT/2000: The handle must have PROCESS_TERMINATE access. <br>For more information, see Process Security and Access Rights. <br><br>所以要先使用 <br>DWORD SetSecurityInfo(<br>&nbsp; HANDLE handle, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle to object<br>&nbsp; SE_OBJECT_TYPE ObjectType, &nbsp; &nbsp; &nbsp; &nbsp; // object type<br>&nbsp; SECURITY_INFORMATION SecurityInfo, // buffer<br>&nbsp; PSID psidOwner, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// new owner SID<br>&nbsp; PSID psidGroup, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// new primary group SID<br>&nbsp; PACL pDacl, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// new DACL<br>&nbsp; PACL pSacl &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // new SACL<br>);<br><br>
 
多人接受答案了。
 
后退
顶部