如何列出所有运行程序进程,并可以杀掉指定的进程,1000分在等各位高手啊!(300分)

  • 主题发起人 主题发起人 壹平
  • 开始时间 开始时间

壹平

Unregistered / Unconfirmed
GUEST, unregistred user!
如何列出所有运行程序进程,并可以杀掉指定的进程,1000分在等各位高手啊!<br><br>delphi 和 C++B 的都可以!
 
翻翻旧贴,N年前就已经讨论过了。
 
我知道呀<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, TLHelp32, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TProcessInfo = record<br>&nbsp; &nbsp; ExeFile: string;<br>&nbsp; &nbsp; ProcessId: DWORD;<br>&nbsp; end;<br>&nbsp; ProcessInfo = ^TProcessInfo;<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure ProcessList(var pList: TList);<br>&nbsp; &nbsp; procedure My_RunFileScan(ListboxRunFile: TListBox);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; Current: TList;<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.ProcessList(var pList: TList);<br>var<br>&nbsp; p: ProcessInfo;<br>&nbsp; ok: Bool;<br>&nbsp; ProcessListHandle: THandle;<br>&nbsp; ProcessStruct: TProcessEntry32;<br>begin<br>&nbsp; PList := TList.Create;<br>&nbsp; PList.Clear;<br>&nbsp; ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; ProcessStruct.dwSize := Sizeof(ProcessStruct);<br>&nbsp; ok := Process32First(ProcessListHandle, ProcessStruct);<br>&nbsp; while Integer(ok) &lt;&gt; 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; new(p);<br>&nbsp; &nbsp; &nbsp; p.ExeFile := ProcessStruct.szExeFile;<br>&nbsp; &nbsp; &nbsp; p.ProcessID := ProcessStruct.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; PList.Add(p);<br>&nbsp; &nbsp; &nbsp; ok := Process32Next(ProcessListHandle, ProcessStruct);<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; h: THandle;<br>&nbsp; a: DWORD;<br>&nbsp; p: PRocessInfo;<br>begin<br>&nbsp; if ListBox1.ItemIndex &gt;= 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; p := Current.Items[ListBox1.ItemIndex];<br>&nbsp; &nbsp; &nbsp; h := openProcess(Process_All_Access, true, p.ProcessID);<br>&nbsp; &nbsp; &nbsp; GetExitCodeProcess(h, a);<br><br>&nbsp; &nbsp; &nbsp; if Integer(TerminateProcess(h, a)) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; My_RunFileScan(ListBox1);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK);<br>end;<br><br>procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox);<br>var<br>&nbsp; i: Integer;<br>&nbsp; p: PRocessInfo;<br>begin<br>&nbsp; current := TList.Create;<br>&nbsp; Current.Clear;<br>&nbsp; ListboxRunFile.Clear;<br>&nbsp; ProcessList(Current);<br>&nbsp; for i := 0 to Current.Count - 1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; new(p);<br>&nbsp; &nbsp; &nbsp; p := Current.Items;<br>&nbsp; &nbsp; &nbsp; ListboxRunFile.Items.Add(p.ExeFile);<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; My_RunFileScan(ListBox1);<br>end;<br><br>end.<br>
 
我有一个程序,专门用来查看、清除程序,DELPHI5下编译通过。
 
uses Tlhelp32;<br><br>function KillTask(ExeFileName: string): integer;<br>const<br>&nbsp; &nbsp; PROCESS_TERMINATE = $0001;<br>var<br>&nbsp; &nbsp; ContinueLoop: BOOL;<br>&nbsp; &nbsp; FSnapshotHandle: THandle;<br>&nbsp; &nbsp; FProcessEntry32: TProcessEntry32;<br>begin<br>&nbsp; &nbsp; result := 0;<br><br>&nbsp; &nbsp; FSnapshotHandle := CreateToolhelp32Snapshot<br>&nbsp; &nbsp; &nbsp; &nbsp; (TH32CS_SNAPPROCESS, 0);<br>&nbsp; &nbsp; FProcessEntry32.dwSize := Sizeof(FProcessEntry32);<br>&nbsp; &nbsp; ContinueLoop := Process32First(FSnapshotHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; FProcessEntry32);<br><br>&nbsp; &nbsp; while integer(ContinueLoop) &lt;&gt; 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpperCase(ExeFileName))<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or (UpperCase(FProcessEntry32.szExeFile) =<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpperCase(ExeFileName))) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := Integer(TerminateProcess(OpenProcess(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PROCESS_TERMINATE, BOOL(0),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FProcessEntry32.th32ProcessID), 0));<br>&nbsp; &nbsp; &nbsp; &nbsp; ContinueLoop := Process32Next(FSnapshotHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FProcessEntry32);<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; CloseHandle(FSnapshotHandle);<br>end;<br><br>
 
记得要引用ToolsAPI: &nbsp; TLHELP32 &nbsp;;<br>procedure TForm1.Button1Click(Sender: TObject);<br>var Entry: TProcessEntry32;<br>&nbsp; &nbsp; h: THandle;<br>begin<br>&nbsp; &nbsp;Listbox1.Items.Clear;<br>&nbsp; &nbsp;h := CreateToolhelp32Snapshot(2,0);<br>&nbsp; &nbsp;if Process32First(h,Entry) then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; Listbox1.Items.Add(Entry.szExeFile + ' &nbsp;- &nbsp;' + IntToStr(Entry.pcPriClassBase));<br>&nbsp; &nbsp; &nbsp; while Process32Next(h,Entry) do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Listbox1.Items.Add(Entry.szExeFile+ ' &nbsp;- &nbsp;' + IntToStr(Entry.pcPriClassBase));<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;CloseHandle(h);<br>end;
 
列出所有的进程!d5指南有,这是部分代码,如果你要,我可以发给你<br>xxhsh@263.net<br>procedure TMainForm.FormCreate(Sender: TObject);<br>begin<br>&nbsp; if Win32Platform = VER_PLATFORM_WIN32_WINDOWS then<br>&nbsp; &nbsp; FWinInfo := TWin9xInfo.Create<br>&nbsp; else if Win32Platform = VER_PLATFORM_WIN32_NT then<br>&nbsp; &nbsp; FWinInfo := TWinNTInfo.Create<br>&nbsp; else<br>&nbsp; &nbsp; raise Exception.Create('This application must be run on Win32');<br>&nbsp; RefreshItemClick(nil);<br>end;<br><br>constructor TWin9xInfo.Create;<br>begin<br>&nbsp; FProcList := TProcList.Create;<br>&nbsp; FWinIcon := LoadImage(0, IDI_WINLOGO, IMAGE_ICON, LR_DEFAULTSIZE,<br>&nbsp; &nbsp; LR_DEFAULTSIZE, LR_DEFAULTSIZE or LR_DEFAULTCOLOR or LR_SHARED);<br>end;<br><br>destructor TWin9xInfo.Destroy;<br>begin<br>&nbsp; DestroyIcon(FWinIcon);<br>&nbsp; if FSnap &gt; 0 then CloseHandle(FSnap);<br>&nbsp; FProcList.Free;<br>&nbsp; inherited Destroy;<br>end;<br><br>procedure TWin9xInfo.FillProcessInfoList(ListView: TListView;<br>&nbsp; ImageList: TImageList);<br>var<br>&nbsp; I: Integer;<br>&nbsp; ExeFile: string;<br>&nbsp; PE: TProcessEntry32;<br>&nbsp; HAppIcon: HIcon;<br>begin<br>&nbsp; Refresh;<br>&nbsp; ListView.Columns.Clear;<br>&nbsp; ListView.Items.Clear;<br>&nbsp; for I := Low(ProcessInfoCaptions) to High(ProcessInfoCaptions) do<br>&nbsp; &nbsp; with ListView.Columns.Add do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if I = 0 then Width := 285<br>&nbsp; &nbsp; &nbsp; else Width := 75;<br>&nbsp; &nbsp; &nbsp; Caption := ProcessInfoCaptions;<br>&nbsp; &nbsp; end;<br>&nbsp; for I := 0 to FProcList.Count - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; PE := PProcessEntry32(FProcList.Items)^;<br>&nbsp; &nbsp; HAppIcon := ExtractIcon(HInstance, PE.szExeFile, 0);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; if HAppIcon = 0 then HAppIcon := FWinIcon;<br>&nbsp; &nbsp; &nbsp; ExeFile := PE.szExeFile;<br>&nbsp; &nbsp; &nbsp; if ListView.ViewStyle = vsList then<br>&nbsp; &nbsp; &nbsp; &nbsp; ExeFile := ExtractFileName(ExeFile);<br>&nbsp; &nbsp; &nbsp; // insert new item, set its caption, add subitems<br>&nbsp; &nbsp; &nbsp; with ListView.Items.Add, SubItems do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Caption := ExeFile;<br>&nbsp; &nbsp; &nbsp; &nbsp; Data := FProcList.Items;<br>&nbsp; &nbsp; &nbsp; &nbsp; Add(IntToStr(PE.cntThreads));<br>&nbsp; &nbsp; &nbsp; &nbsp; Add(IntToHex(PE.th32ProcessID, 8));<br>&nbsp; &nbsp; &nbsp; &nbsp; Add(IntToHex(PE.th32ParentProcessID, 8));<br>&nbsp; &nbsp; &nbsp; &nbsp; if ImageList &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImageIndex := ImageList_AddIcon(ImageList.Handle, HAppIcon);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; if HAppIcon &lt;&gt; FWinIcon then DestroyIcon(HAppIcon);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br><br>
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp; i:integer;<br>&nbsp; &nbsp; hCurrentWindow: HWnd;<br>&nbsp; &nbsp; szText: array[0..254] of char;<br>&nbsp; begin<br>&nbsp; &nbsp; ListBox1.Items.Clear;<br>&nbsp; &nbsp; hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);<br>&nbsp; &nbsp; i:=hCurrentWindow;<br>&nbsp; &nbsp; While hCurrentWindow &lt;&gt; 0 Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; If GetWindowText(hCurrentWindow, @szText, 255) &gt; 0 Then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListBox1.Items.add(Strpas(@szText));<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp;Strpas(@szText)='Myform' then<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; postmessage(hCurrentWindow,WM_SYSCOMMAND,SC_CLOSE,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hCurrentWindow := GetNextWindow(hCurrentWindow, GW_HWNDNEXT);<br>&nbsp; &nbsp; End;<br>&nbsp; &nbsp; end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; &nbsp; i:integer;<br>&nbsp; &nbsp; hCurrentWindow: HWnd;<br>&nbsp; &nbsp; szText: array[0..254] of char;<br>&nbsp; &nbsp; ProTitle:string;<br>&nbsp; begin<br>&nbsp; &nbsp; ProTitle:=Listbox1.Items[Listbox1.ItemIndex];<br>&nbsp; &nbsp; hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);<br>&nbsp; &nbsp; i:=hCurrentWindow;<br>&nbsp; &nbsp; While hCurrentWindow &lt;&gt; 0 Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; If GetWindowText(hCurrentWindow, @szText, 255) &gt; 0 Then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if &nbsp;Strpas(@szText)=ProTitle then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;postmessage(hCurrentWindow,WM_SYSCOMMAND,SC_CLOSE,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listbox1.Items.Delete(listbox1.ItemIndex);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hCurrentWindow := GetNextWindow(hCurrentWindow, GW_HWNDNEXT);<br>&nbsp; &nbsp; End;<br>&nbsp; &nbsp; end;<br><br>end.<br>
 
zlj555的回答是正确的,不过要注意,在结束进程时,要这样写:<br>TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,p.th32ProcessID),0);<br>总体的思路就是:<br>1:建立系统快照<br>2:process32first 得到TProcessEntry32结构<br>3:process32next<br>4:termenate()<br>值得注意的是TProcessEntry32中的th32ProcessID并不是进程的句柄。
 
为什么不用Exitprocess()?
 
用sendmessage(hwnd,wm_close,0,0)
 
还要注意权限问题,有些进程没有足够的权限是杀不掉的。
 
我知道怎么做,给我Email &nbsp;MaximChen@hotmail.com<br>准备给我加分吧。
 
靠~~~知道就帖出来呀。。。。不帖不要给分
 
另外发现的一种<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp;StdCtrls;<br><br>type<br>&nbsp;TForm1 = class(TForm)<br>&nbsp; &nbsp;Button1: TButton;<br>&nbsp; &nbsp;ListBox1: TListBox;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp;procedure ListBox1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp;private<br>&nbsp; &nbsp;{ Private declarations }<br>&nbsp;public<br>&nbsp; &nbsp;{ Public declarations }<br>&nbsp;end;<br><br>var<br>&nbsp;Form1: TForm1;<br>&nbsp; Wnd: HWND;<br>implementation<br><br>{$R *.DFM}<br><br>Function EnumWindowsProc (Wnd: HWND; lb: TListbox): BOOL; stdcall;<br>var<br>&nbsp;caption: Array [0..128] of Char;<br>begin<br>&nbsp;Result := True;<br>&nbsp;if { skip invisible windows }<br>&nbsp; &nbsp; IsWindowVisible(Wnd) and<br>&nbsp; &nbsp; { only process truly top-level windows. GetWindowLong must be used, not<br>GetParent }<br>&nbsp; &nbsp; ((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or<br>&nbsp; &nbsp; &nbsp;(HWND(GetWindowLong(Wnd, GWL_HWNDPARENT)) = GetDesktopWindow)) and<br>&nbsp; &nbsp; { skip WS_EX_TOOLWINDOW windows }<br>&nbsp; &nbsp; ((GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW) = 0)<br>&nbsp;then begin<br>&nbsp; &nbsp;SendMessage( Wnd, WM_GETTEXT, Sizeof( caption ), integer(@caption));<br>&nbsp; &nbsp;lb.Items.AddObject( caption, TObject( Wnd ));<br>&nbsp;end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp;listbox1.clear;<br>&nbsp;EnumWindows( @EnumWindowsProc, integer( listbox1 ));<br>end;<br><br>procedure TForm1.ListBox1Click(Sender: TObject);<br>var<br>&nbsp;theClassname: Array [0..128] of Char;<br><br>&nbsp;tid, pid: DWORD;<br>&nbsp;intExitCode:DWORD;<br>begin<br>&nbsp;With Sender As TListbox Do Begin<br>&nbsp; &nbsp;If ItemIndex &gt;= 0 Then Begin<br>&nbsp; &nbsp; &nbsp;Wnd:= HWND(Items.Objects[ itemindex ]);<br>&nbsp; &nbsp; &nbsp;If Wnd &lt;&gt; 0 Then Begin<br>&nbsp; &nbsp; &nbsp; &nbsp;Windows.GetClassname( Wnd, theClassname, Sizeof( classname ));<br>&nbsp; &nbsp; &nbsp; &nbsp;tid := GetWindowThreadProcessID( Wnd, @pid );<br>&nbsp; &nbsp; &nbsp; &nbsp;label1.caption :=<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Format(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'HWND: %8.8x'#13#10+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Class: %s'#13#10+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Process ID: %8.8x'#13#10+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Thread ID: %8.8x',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[Wnd, theClassname, pid, tid] );<br>&nbsp; &nbsp; &nbsp;End;<br>&nbsp; &nbsp;End;<br>&nbsp;End;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; PostMessage(Wnd, WM_CLOSE, 0, 0);<br>&nbsp; Button1.Click;<br>end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部