转(leebons)<br><br>下面是我写的一段源代码,只要你把你想要终止的程序名(包括路径)写入<br>D:/Program Files/text1.txt这个文件中(注意每行写一个程序名).那么这个<br>程序无法运行下去.程序里有详细的注释.祝你好运!<br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,tlhelp32,stdCtrls, ExtCtrls;//注意加上tlhelp32这个单元;<br>type <br> TForm1 = class(TForm)<br> ListBox1: TListBox;<br> Button1: TButton;<br> Timer1: TTimer;<br> ListBox2: TListBox;<br> procedure FormCreate(Sender: TObject);<br> procedure Timer1Timer(Sender: TObject);<br> private<br> procedure processlist(var prolist:tlist);//自定义函数列举所有的进程;<br> procedure exitcode ;//自定义函数终止进程;<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br>type tproinfo=record<br>filename:string;<br>proid:dword;<br>end;proinfo=^tproinfo;//自定义记录用来记录进程的文件名和ID;<br><br>var<br> Form1: TForm1;<br> curr:tlist;<br> temp,b,a1:integer;<br>implementation<br><br>{$R *.DFM}<br><br><br>procedure TForm1.processlist(var prolist: tlist);<br>var p
roinfo;<br> ok:bool;<br> prolisthandle:thandle;<br> prostruct:tprocessentry32; //记录进程的数据结构;<br>begin<br>prolist:=tlist.Create ;<br>prolist.Clear ;<br>prolisthandle:=createtoolhelp32snapshot(th32cs_snapprocess,0);<br>prostruct.dwSize :=sizeof(prostruct);<br>ok:=process32first(prolisthandle,prostruct);//发现第一个进程;<br>while integer(ok)<>0 do<br>begin<br>new(p);<br>p.filename :=prostruct.szExeFile ;<br>p.proid :=prostruct.th32ProcessID ;<br>prolist.Add (p);<br>ok:=process32next(prolisthandle,prostruct);//发现下一个进程;<br>end;<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>a:string;<br>f:textfile;<br>begin<br>listbox1.Clear;<br>listbox2.Clear;<br>if fileexists('D:/Program Files/text1.txt') then<br>begin //该文件记录你所想禁止运行的程序的路径;<br>assignfile(f,'D:/Program Files/text1.txt');<br>reset(f);<br>while not eof(f) do<br>begin<br>readln(f,a);<br>a:=uppercase(a); //转化成大写字母;<br>listbox2.Items.Add (a); //记录所有被禁止运行程序的路径;<br>end;<br>closefile(f)<br>end<br>else application.Terminate ;<br><br>end;<br><br><br>procedure Tform1.exitcode;<br>var h:thandle;<br>a:dword;<br>p
roinfo;<br><br>begin<br>begin<br>p:=curr.items
; //指向禁止运行的进程的数据结构;<br>h:=openprocess(process_all_access,true,p.proid);<br>getexitcodeprocess(h,a); //得到进程退出代码;<br>terminateprocess(h,a) ; //终止进程<br>end;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var i:integer;<br>proinfo;<br>begin<br>listbox1.Clear ;<br>processlist(curr); //调用进程列举的函数;<br>for i:=0 to curr.Count-1 do<br>begin<br>new(p);<br>p:=curr.Items;<br>listbox1.Items.Add(p.filename); //记录所有的进程的路径;<br>end; //listbox2是记录所有禁止运行的程序的路径;<br>for i:=0 to listbox2.Items.Count-1 do<br>if (listbox1.Items.IndexOf(listbox2.Items.Strings)>=0) then<br>begin<br>b:=listbox1.Items.IndexOf(listbox2.Items.Strings);<br>exitcode;//调用终止进程的函数;<br>end;<br>end;<br>end.