转贴一个, 参考一下吧<br><br>unit Find_Unit;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, TLHelp32, ComCtrls;<br><br>type<br> TProcessInfo=Record<br> ExeFileName:String;<br> ProcessID
Word;<br> end;<br><br>type<br> TFindForm = class(TForm)<br> Button1: TButton;<br> Edit1: TEdit;<br> Edit2: TEdit;<br> SB: TStatusBar;<br> Label1: TLabel;<br> Label2: TLabel;<br> procedure Button1Click(Sender: TObject);<br> function findthread(threadname:string):boolean;<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> FindForm: TFindForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TFindForm.Button1Click(Sender: TObject);<br>var hfile:thandle; r:boolean; tf:string;<br>begin<br> r:=false; //查找空间变量是否存在 eg: in win2000 "cdrom0" is a space value<br> hfile:=createfile(pchar(edit1.Text),Generic_Read or<br> Generic_Write,File_Share_Read or File_Share_Write,<br> nil,Open_Existing,File_Attribute_Normal,0);<br> if hfile<>Invalid_Handle_Value then<br> begin<br> CloseHandle(hfile);<br> r:=true;<br> end;<br> if r then sb.Panels[0].Text:='Space Find!' else sb.Panels[0].Text:='Space Not Found!';<br> tf:=edit2.text;<br> if pos('.',tf)=0 then tf:=tf+'.exe';<br> if findthread(tf) then sb.Panels[1].Text:='Thread Find!' else sb.Panels[1].Text:='Thread Not Found!';<br>end;<br><br>function TFindForm.findthread(threadname: string): boolean;<br>var //关键的过程<br> p:TProcessInfo;<br> OK:Bool;<br> ProcessListHandle:THandle;<br> ProcessStruct:TProcessEntry32;<br>begin<br> result:=false;<br> ProcessListHandle:=CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);<br> ProcessStruct.dwSize:=SizeOf(ProcessStruct);<br> OK:=Process32First(ProcessListHandle,ProcessStruct);<br> while Integer(OK)<>0 do<br> begin<br> p.ExeFileName:=ProcessStruct.szExeFile;<br>// p.ProcessID:=ProcessStruct.th32ProcessID;<br> OK:=Process32Next(ProcessListHandle,ProcessStruct);<br> if uppercase(p.ExeFileName)=uppercase(threadname) then begin result:=true; exit; end;<br> end;<br> closehandle(ProcessListHandle); <br>end;<br><br>end.<br><br><br><br>