如何得到系统的所有进程,所有句炳?(100分)

  • 主题发起人 主题发起人 路远
  • 开始时间 开始时间

路远

Unregistered / Unconfirmed
GUEST, unregistred user!
来自:tinytao, 时间:2001-6-7 12:26:00, ID:557402 <br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; Grids, StdCtrls, ExtCtrls,<br>&nbsp; tlhelp32; //记得加这个<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><br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; StringGrid1: TStringGrid;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure FormActivate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure Initialize;<br>&nbsp; &nbsp; procedure RefreshList; &nbsp; //刷新<br>&nbsp; &nbsp; procedure ProcessList(var PList:TList); &nbsp;//得进程列表<br>&nbsp; &nbsp; function KillCourse(Item:integer):String; &nbsp;//杀进程<br>&nbsp; public<br>&nbsp; &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.FormCreate(Sender: TObject);<br>var<br>&nbsp; temp:integer;<br>begin<br>&nbsp; //这两句可不要,第一句,不在任务栏显示<br>&nbsp; //第二句屏蔽Ctrl+alt+Del<br>&nbsp; &nbsp; SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);<br>&nbsp; SystemParametersInfo(Spi_screensaverrunning,1,@temp,0);<br><br>&nbsp; Current:=TList.Create;<br>&nbsp; Initialize;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>var<br>&nbsp; temp:integer;<br>begin<br>&nbsp; Current.Free;<br>&nbsp; //恢复Ctrl+alt+Del<br>&nbsp; SystemParametersInfo(Spi_screensaverrunning,0,@temp,0);<br>end;<br><br>procedure TForm1.FormActivate(Sender: TObject);<br>begin<br>&nbsp; //这句就看你了,调试就不要加了<br>// &nbsp;ShowWindow(Handle, SW_HIDE);<br>end;<br><br>procedure TForm1.Initialize;<br>begin<br>&nbsp; StringGrid1.FixedCols:=0;<br>&nbsp; StringGrid1.FixedRows:=1;<br>&nbsp; StringGrid1.ColCount:=3;<br>&nbsp; StringGrid1.Options:=[goFixedVertLine,goFixedHorzLine,goRangeSelect,goRowSelect];<br>&nbsp; StringGrid1.ColWidths[0]:=70;<br>&nbsp; StringGrid1.ColWidths[1]:=100;<br>&nbsp; StringGrid1.ColWidths[2]:=StringGrid1.Width-200;<br>&nbsp; StringGrid1.DefaultRowHeight:=14;<br>&nbsp; StringGrid1.Cells[0,0]:='ID';<br>&nbsp; StringGrid1.Cells[1,0]:='进程';<br>&nbsp; StringGrid1.Cells[2,0]:='路径';<br><br>&nbsp; RefreshList;<br>end;<br><br>procedure TForm1.ProcessList(var PList: TList);<br>var<br>&nbsp; &nbsp;p:processinfo;<br>&nbsp; &nbsp;Ok:Bool;<br>&nbsp; &nbsp;ProcessListHandle:Thandle;<br>&nbsp; &nbsp;Processstruct:TProcessentry32;<br>begin<br>&nbsp; &nbsp;PList.Clear;<br>&nbsp; &nbsp;ProcessListHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; &nbsp;Processstruct.dwSize:=sizeof(Processstruct);<br>&nbsp; &nbsp;ok:=process32first(ProcessListHandle,processstruct);<br>&nbsp; &nbsp;while Ok do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;new(p);<br>&nbsp; &nbsp; &nbsp; &nbsp;p.ExeFile:=Processstruct.szExeFile;<br>&nbsp; &nbsp; &nbsp; &nbsp;p.ProcessId:=Processstruct.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; &nbsp;PList.Add(p);<br>&nbsp; &nbsp; &nbsp; &nbsp;ok:=Process32Next(ProcessListHandle,Processstruct );<br>&nbsp; &nbsp; &nbsp; end;<br>end;<br><br>procedure TForm1.RefreshList;<br>var<br>&nbsp; i:integer;<br>&nbsp; p:processinfo;<br>begin<br>&nbsp; &nbsp;ProcessList(current);<br><br>&nbsp; &nbsp;StringGrid1.RowCount:=current.Count+1;<br>&nbsp; &nbsp;for i:=1 to StringGrid1.RowCount do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;StringGrid1.Cells[0,i]:='';<br>&nbsp; &nbsp; &nbsp;StringGrid1.Cells[1,i]:='';<br>&nbsp; &nbsp; &nbsp;StringGrid1.Cells[2,i]:='';<br>&nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;for i:=1 to current.Count do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;p:=current.Items[i-1];<br>&nbsp; &nbsp; &nbsp; &nbsp;StringGrid1.Cells[0,i]:=IntToHex(p.ProcessId,8);<br>&nbsp; &nbsp; &nbsp; &nbsp;StringGrid1.Cells[1,i]:=ExtractFileName(P.ExeFile);<br>&nbsp; &nbsp; &nbsp; &nbsp;StringGrid1.Cells[2,i]:=ExtractFilePath(p.ExeFile);<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; str:string;<br>begin<br>&nbsp; if StringGrid1.Selection.Top&gt;0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;str:=KillCourse(StringGrid1.Selection.Top-1);<br>&nbsp; &nbsp; &nbsp; &nbsp;if str&lt;&gt;'' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.MessageBox(Pchar(Str),'杀杀杀!',64);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RefreshList;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>function TForm1.KillCourse(Item: integer): String;<br>var<br>&nbsp; h:Thandle;<br>&nbsp; a:DWord;<br>&nbsp; p:ProcessInfo;<br>&nbsp; Name:string;<br>begin<br>&nbsp; Result:='';<br>&nbsp; try<br>&nbsp; &nbsp; p:=current.Items[Item];<br>&nbsp; &nbsp; Name:=ExtractFileName(P.ExeFile);<br>&nbsp; &nbsp; h:=OpenProcess(Process_All_Access,True,p.ProcessId);<br>&nbsp; &nbsp; GetExitCodeProcess(h,a);<br>&nbsp; &nbsp; if TermInateprocess(h,a) then<br>&nbsp; &nbsp; &nbsp;Result:=Name;<br>&nbsp; finally<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; RefreshList;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br>&nbsp; i:integer;<br>&nbsp; p:ProcessInfo;<br>begin<br>&nbsp; ProcessList(current);<br>&nbsp; i:=0;<br>&nbsp; repeat<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; p:=current.Items;<br>&nbsp; &nbsp; &nbsp; &nbsp; if Pos('你要杀的',P.ExeFile)&gt;0 then &nbsp;//注意:不要把你的系统也杀了<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KillCourse(i);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessList(current);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i:=-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; i:=i+1;<br>&nbsp; &nbsp; end;<br>&nbsp; until I=current.Count;<br>end;<br><br>end.<br><br>&nbsp;<br>
 
自已看吧,应该看的懂!
 
我是这样写的,想得到窗口的类:<br>procedure TFormCallback.Button1Click(Sender: TObject);<br><br>&nbsp;var<br>&nbsp; A: string;<br>begin<br>&nbsp; &nbsp;SetLength (A, 100);<br>&nbsp; GetClassName (Hwnd,a ,100);//过不去?<br>&nbsp; FormCallBack.ListBox1.Items.Add (<br>&nbsp; IntToStr (Hwnd) + ': ' + a);<br>&nbsp; Result := True;<br>end;<br>得到获得窗口标识<br>var<br>b:sting;<br>begin<br>b:=GetWindowLong(hWnd,GWL_ID);<br>FormCallBack.ListBox1.Items.Add (<br>&nbsp; IntToStr (Hwnd) + ': ' + b);<br>&nbsp; Result := True;<br><br>我是想得到所有的进程的窗口名,窗口的类,窗口标识<br>就像delphi带的那个WinSight32一样,那唯有类似WinSight32的源码?<br><br>
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; ListBox2: TListBox;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(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>function recall(handle:hwnd):boolean;stdcall; &nbsp;//:回调函数<br>var lp1,lp2:array[0..255]of char;i,j:integer;s1,s2:string;<br>begin<br>&nbsp;i:=getwindowtext(handle,lp1,255);<br>&nbsp;j:=getclassname(handle,lp2,255);<br>&nbsp;s1:=copy(lp1,1,i);s2:=copy(lp2,1,j);<br>&nbsp;form1.ListBox1.AddItem(s1,form1); &nbsp; &nbsp;//:listbox1显示窗口标题,listbox2显示相应类名<br>&nbsp;form1.listbox2.additem(s2,form1);<br>&nbsp;recall:=true;<br>&nbsp;{HANDLE即为窗口的句柄,你可以在回调函数<br>&nbsp;中对handle操作}<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp; var handle:hwnd;<br>begin<br>listbox1.Clear;<br>EnumWindows(@recall,handle); &nbsp; //:列举窗口<br>end;<br>end.<br>&nbsp;如果要原程序,留下Emai地址。
 
var<br>&nbsp; handlea:HWnd;<br>&nbsp; szText: array[0..254] of char;<br>&nbsp; t:integer;<br>begin<br>&nbsp; ComboBox1.Clear;<br>&nbsp; t:=0;<br>&nbsp; handlea := GetWindow(Handle, GW_HWNDFIRST);<br>&nbsp; while handlea &lt;&gt; 0 do<br>&nbsp; begin<br>&nbsp; if GetWindowText(handlea, @szText, 255)&gt;0 then<br>&nbsp; &nbsp;if strpas(@szText)&lt;&gt;'Default IME' then<br>&nbsp; &nbsp; &nbsp; ComboBox1.items.Add(StrPas(@szText));<br>&nbsp; &nbsp;handlea:=GetWindow(handlea, GW_HWNDNEXT);<br>&nbsp; &nbsp;t:=t+1;<br>&nbsp; end;<br>end;//得到窗体的句柄柄显示窗体名称<br>
 
&nbsp;var p : pProcessInfo;<br>&nbsp; &nbsp; &nbsp; ContinueLoop:BOOL;<br>&nbsp; &nbsp; &nbsp; FSnapshotHandle:THandle;<br>&nbsp; &nbsp; &nbsp; FProcessEntry32:TProcessEntry32;<br>&nbsp; &nbsp; &nbsp; t:integer;<br>begin<br>&nbsp; ComboBox2.Clear;<br>&nbsp; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);<br>&nbsp; ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);<br>&nbsp; t:=0;<br>&nbsp; while integer(ContinueLoop)&lt;&gt;0 do<br>&nbsp; begin<br>&nbsp; &nbsp; New(p);<br><br>&nbsp; &nbsp; p.ExeFile := FProcessEntry32.szExeFile;<br>&nbsp; &nbsp; p.ProcessID := FProcessEntry32.th32ProcessID;<br>&nbsp; &nbsp; proID[t]:=p.ProcessID;<br>&nbsp; &nbsp; t:=t+1;<br>&nbsp; &nbsp; ComboBox2.Items.Add(p.ExeFile);<br>&nbsp; &nbsp; ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);<br>&nbsp; end;<br>&nbsp; ComboBox2.ItemIndex := 0;<br>end;<br>显示做有进程,并显示应用程序的名称
 
关键函数:EnumWindows
 
多人接受答案了。
 
后退
顶部