{ Windows calls this function, passing in Handle a reference
to each alive window. Param is not used in this example. }
function EnumWinProc(Handle: HWnd; Param: Longint): Boolean;
Stdcall; { Use this, not export, in Windows 95, 98 & NT }
var
Sz: array[0 .. 132] of Char; {Holds result of GetWindowText}
begin
Result := True; { Always successful }
{ Call Windows to obtain each window's caption, and then
add the returned string to the form's ListBox. }
if GetWindowText(Handle, Sz, Sizeof(Sz)) <> 0 then
MainForm.ListBox1.Items.Add(StrPas(Sz));
end;{ Enumerate all alive windows by passing to Windows the
address of the preceding callback function. }
procedure TMainForm.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWinProc, 0); { 0 is an unused parameter }
end;
to each alive window. Param is not used in this example. }
function EnumWinProc(Handle: HWnd; Param: Longint): Boolean;
Stdcall; { Use this, not export, in Windows 95, 98 & NT }
var
Sz: array[0 .. 132] of Char; {Holds result of GetWindowText}
begin
Result := True; { Always successful }
{ Call Windows to obtain each window's caption, and then
add the returned string to the form's ListBox. }
if GetWindowText(Handle, Sz, Sizeof(Sz)) <> 0 then
MainForm.ListBox1.Items.Add(StrPas(Sz));
end;{ Enumerate all alive windows by passing to Windows the
address of the preceding callback function. }
procedure TMainForm.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWinProc, 0); { 0 is an unused parameter }
end;
procedure TForm1.Timer1Timer(Sender: TObject);
Var
FrmHandle:HWND;
Len:Integer;
Str:array[0..255] Of Char;
Begin
ListBox1.Clear;
FrmHandle:=GetWindow(Handle,GW_HWNDFIRST);
While FrmHandle<>0 Do
Begin
If GetWindowText(FrmHandle,@Str,255)>0 Then
ListBox1.Items.Add(StrPas(@Str));
FrmHandle:=GetWindow(FrmHandle,GW_HWNDNEXT);
End;
End;
关闭进程:
procedure TForm1.Button1Click(Sender: TObject);
Var
Str:pchar;
FindHandle:HWND;
Begin
Str:=Pchar(ListBox1.Items[ListBox1.itemindex]);
FindHandle:=FindWindow(nil,Str);
If FindHandle<>0 Then
SendMessage(FindHandle,WM_CLOSE,0,0);
End;
CreateToolhelp32Snapshot//必须先调用它.
Process32First+Process32Next//得到系统所有进程信息;
Thread32First+Thread32Next//得到系统所有线程信息;
以上函数在TlHelp32单元.
然后在用OpenProcess和OpenThread得到它们的句柄.
unit MainForm;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
TlHelp32, StdCtrls, ComCtrls, ExtCtrls;type
TfrmMain = class(TForm)
pnlInfo: TPanel;
lvThreads: TListView;
0A Splitter1: TSplitter;
0A pnlButtons: TPanel;
btnRefresh: TButton;
btnClose: TButton;
lvProcs: TListView;
procedure btnRefreshClick(Sender: TObject);
procedure lvProcsSelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
0A procedure btnCloseClick(Sender: TObject);
private
procedure FillThreadList(ProcID:Integer);
public
end;var
frmMain: TfrmMain;implementation{$R *.DFM}
procedure TfrmMain.FillThreadList(ProcID: Integer);
var
hSnapshot:THandle;
ThreadEntry:TThreadEntry32;0D
RetVal:Bool;
LvItem:TListItem;
begin
lvThreads.Items.Clear;
0A hSnapshot:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS or TH32CS_SNAPTHREAD,GetCurrentProcessId);
ThreadEntry.dwSize := Sizeof(ThreadEntry);
RetVal := Thread32First(hSnapshot,ThreadEntry);
while RetVal do
begin
if ThreadEntry.th32OwnerProcessID = Integer(ProcID) then
begin
LvItem := lvThreads.Items.Add;
LvItem.Data := Pointer((ThreadEntry.th32ThreadID));
LvItem.Caption := IntToStr(ThreadEntry.th32ThreadID);
end;
RetVal := Thread32Next(hSnapshot,ThreadEntry);
end;
CloseHandle(hSnapshot);
end;procedure TfrmMain.btnRefreshClick(Sender: TObject);
var
hSnapshot:THandle;
ProcEntry:TProcessEntry32;0D
RetVal:Bool;
LvItem:TListItem;
TvNode:TTreeNode;
ThreadList:TList;
begin
lvProcs.Items.Clear;
hSnapshot:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS or TH32CS_SNAPTHREAD,GetCurrentProcessId);
ProcEntry.dwSize := Sizeof(ProcEntry);
RetVal := Process32First(hSnapshot,ProcEntry);
while RetVal do
begin
LvItem :=lvProcs.Items.Add;
LvItem.Caption :=(ProcEntry.szExeFile);
LvItem.SubItems.Add(IntToStr(ProcEntry.th32ProcessID));0D
LvItem.SubItems.Add(IntToStr(ProcEntry.th32ParentProcessID));
LvItem.SubItems.Add(IntToStr(ProcEntry.cntThreads));
0A LvItem.Data:=(Pointer(ProcEntry.th32ProcessID));
RetVal := Process32Next(hSnapshot,ProcEntry);
end;
CloseHandle(hSnapShot);
0Aend;procedure TfrmMain.lvProcsSelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
begin
if Selected then
begin
FillThreadList(Integer(Item.Data));
end;
end;procedure TfrmMain.btnCloseClick(Sender: TObject);
begin
Close;
end;
end.上面的代码只得到了进程ID和线程ID,你再用OpenProcess和OpenThread去得到
object frmMain: TfrmMain
Left = 187
Top = 326
Width = 545
Height = 467
Caption = '进程/线程查看'
Color = clBtnFace
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
OnCreate = btnRefreshClick
PixelsPerInch = 96
TextHeight = 12
object pnlInfo: TPanel
Left = 0
Top = 0
Width = 537
Height = 399
Align = alClient
TabOrder = 0
object Splitter1: TSplitter
Left = 389
Top = 1
Width = 3
Height = 397
Cursor = crHSplit
Align = alRight
end
object lvThreads: TListView
Left = 392
Top = 1
Width = 144
Height = 397
Align = alRight
Columns = <
item
AutoSize = True
Caption = '线程ID'
end>
HideSelection = False
ReadOnly = True
RowSelect = True
SortType = stText
TabOrder = 0
ViewStyle = vsReport
end
object lvProcs: TListView0D
Left = 1
Top = 1
Width = 388
Height = 397
Align = alClient
Columns = <
item
AutoSize = True
Caption = '进程映象名'
end
item
Caption = '进程ID'
end
item
Caption = '父进程ID'
Width = 60
end
item
Caption = '线程数'
end>
HideSelection = False
ReadOnly = True
RowSelect = True
SortType = stText
TabOrder = 1
ViewStyle = vsReport
OnSelectItem = lvProcsSelectItem
end
end
object pnlButtons: TPanel
Left = 0
Top = 399
Width = 537
Height = 41
Align = alBottom
BevelOuter = bvLowered
TabOrder = 1
object btnRefresh: TButton
Left = 312
Top = 8
Width = 115
Height = 25
Anchors = [akTop, akRight]
Caption = '刷新进程列表'
TabOrder = 0
OnClick = btnRefreshClick
end
object btnClose: TButton0D
Left = 440
Top = 8
Width = 75
Height = 25
Anchors = [akTop, akRight]
Caption = '关闭'
TabOrder = 1
OnClick = btnCloseClick
end
end
end