关于如何取得系统中程序的线程???(80分)

  • 主题发起人 主题发起人 dh0497
  • 开始时间 开始时间
D

dh0497

Unregistered / Unconfirmed
GUEST, unregistred user!
是这样的:比如说我打开一个winrar.exe,如何在程序里面获得这个线程的状态:存在或关闭。
有相关的API函数吗?不吝赐教~~~~~
 
使用ToolHelp API:
CreateToolhelp32Snapshot//必须先调用它.
Process32First+Process32Next//得到系统所有进程信息;

Thread32First+Thread32Next//得到系统所有线程信息;

以上函数在TlHelp32单元.
然后在用OpenProcess和OpenThread得到它们的句柄.
---------------------------------------------------------------

给你参考一个例子:
unit MainForm;


interface

uses
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去得到

---------------------------------------------------------------
上面是一个窗体的代码(MainForm.pas),索性把窗体文件(MainForm.DFM)的内容也贴出来,你就可以直接加入到你的工程里去试一试了.

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
 
Thread32First+Thread32Next//得到系统所有线程信息;
 
后退
顶部