我以前写的一个类,完全合你的要求,从但是我不保证所有程序都能取得信息,也不保证程序的健壮性。<br>-----------------------------------------------------<br>unit ProcessFunc;<br><br>interface<br>uses<br> Windows,Classes,SysUtils,Types,TLHelp32;<br>Type<br> TProcessListItem= class<br> Size: Longword ; //Longword :无符号32位整数。<br> Usage: Longword ;<br> ProcessID: Longword ; // this process<br> DefaultHeapID: Longword ;<br> ModuleID: Longword ; // associated exe<br> Threads: Longword ;<br> ParentProcessID: Longword ; // this process's parent process<br> PriClassBase: Longint; // Base priority of process's threads<br> Flags: Longword ;<br> ExeFile:string;<br> ModuleList:Tstrings;<br> private<br> miHandle:THandle;<br> function GetHandle: THandle;<br> published<br> property Handle :Thandle read GetHandle;<br> constructor Create;<br> destructor Destroy;override;//销毁器<br> Function GetModuleList:TStrings;<br> Function Kill:integer;<br> end;<br> TProcessList = Class<br> Count:integer;<br> Hand : THandle;<br> ProcessItem :array [0..255] of TprocessListItem;<br> MyProcess:TprocessListItem;<br> constructor Create;<br> destructor Destroy;override;//销毁器<br> <br><br> Function GetList :integer;<br><br> end;<br> <br> Function KillProcess(FormTitle:string):integer; overload;<br> Function KillProcess(Handle:THandle):integer; overload;<br> Function KillProcess2(ProcessID
WORD):integer; overload;<br><br><br>implementation<br><br>Function KillProcess(FormTitle:string):integer;<br>var<br> H:THandle;<br> P
WORD;<br> TerminateResult:LongBool;<br>begin<br> H:=FindWindow(nil,PAnsiChar(FormTitle));<br> if H<>0 then<br> begin<br> GetWindowThreadProcessId(H,@P);<br> if P<>0 then<br> TerminateResult:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);<br> if not TerminateResult then<br> begin<br> //GetLastError();<br> end;<br> end;<br>end;<br>Function KillProcess(Handle:THandle):integer;<br>var<br> P
WORD;<br> TerminateResult:LongBool;<br>begin<br> if Handle<>0 then<br> begin<br> GetWindowThreadProcessId(Handle,@P);<br> if P<>0 then<br> TerminateResult:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);<br> if not TerminateResult then<br> begin<br> Result := -1;<br> //GetLastError();<br> exit;<br> end;<br> end else begin<br> Result := -1;<br> end;<br><br> Result:=0;<br>end;<br><br>Function KillProcess2(ProcessID
WORD):integer;<br>var<br> TerminateResult:LongBool;<br>begin<br> TerminateResult:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,ProcessID),$FFFFFFFF);<br> if not TerminateResult then<br> begin<br> Result := -1;<br> //GetLastError();<br> exit;<br> end;<br> Result :=0;<br>end;<br><br>{ TProcessListItem }<br><br>constructor TProcessListItem.Create;<br>begin<br> ModuleList:= TStringList.Create;<br>end;<br><br>destructor TProcessListItem.Destroy;<br>begin<br> ModuleList.Destroy;<br> if self.Handle >0 then CloseHandle(self.Handle );<br> inherited;<br>end;<br><br>function TProcessListItem.GetHandle: THandle;<br>begin<br> if self.miHandle =0 then<br> self.miHandle :=CreateToolHelp32SnapShot(TH32CS_SNAPModule,self.ProcessID );<br> result:=self.miHandle ;<br><br>end;<br><br>function TProcessListItem.GetModuleList: Tstrings;<br>var<br> me:MODULEENTRY32;<br> found:boolean;<br> strings:string;<br><br>begin<br> self.ModuleList.Clear;<br> me.dwSize:=sizeof(ModuleEntry32);<br><br> found:=Module32First(self.Handle,me);<br> while found do<br> begin<br> if me.th32ProcessID =self.ProcessID then<br> begin<br> self.ModuleList.Add( me.szExePath);<br> end;<br> found:=Module32Next(self.Handle ,me);<br> end;<br> CloseHandle(self.miHandle);<br> self.miHandle :=0;<br> Result:=ModuleList;<br><br><br>end;<br><br>function TProcessListItem.Kill: integer;<br>begin<br> if TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,self.ProcessID),$FFFFFFFF)<br> then<br> begin<br> Result := 0;<br> end;<br> Result :=-1;<br>end;<br><br>{ TProcessList }<br><br>constructor TProcessList.Create;<br>begin<br><br>end;<br><br>destructor TProcessList.Destroy;<br>var<br> i:integer;<br>begin<br> for i := 0 to self.Count -1 do self.ProcessItem
.Destroy;<br> inherited;<br>end;<br><br>function TProcessList.GetList: integer;<br>var<br> lppe: TProcessEntry32;<br> found : boolean;<br><br> iCount,i:integer;<br> //me:MODULEENTRY32;<br>begin<br> for i := 0 to self.Count -1 do self.ProcessItem.Destroy;<br> iCount:= 0;<br> if hand =0 then<br> Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);<br> found := Process32First(Hand,lppe);<br> while found do //<br> begin<br> self.ProcessItem[iCount]:= TProcessListItem.Create;<br><br> with self.ProcessItem[iCount] do<br> begin<br><br> Size := lppe.dwSize;<br> Usage:= lppe.cntUsage ;<br> ProcessID:= lppe.th32ProcessID ;<br> DefaultHeapID:= lppe.th32DefaultHeapID ;<br> ModuleID:= lppe.th32ModuleID ;<br> Threads:= lppe.cntThreads ;<br> ParentProcessID:= lppe.th32ParentProcessID ;<br> PriClassBase:= lppe.pcPriClassBase ;<br> Flags:= lppe.dwFlags ;<br> ExeFile := StrPas(lppe.szExeFile );<br><br> end;<br> inc(iCount);<br> found := Process32Next(Hand,lppe);<br> end;<br><br> self.Count := iCount;<br><br>end;<br><br><br>end.