怎样得到一个应用程序里的线程数?

bailee 2001-06-30 11:34:09
怎样得到一个应用程序里的线程数?
还有
怎样得到某个应用程序里的线程?
...全文
304 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bailee 2001-07-02
  • 打赏
  • 举报
回复
to ey4s(雏鹰):
首先,谢谢你的回答。
能不能举个例子?
得到应用程序的线程是指得到一个应用程序里的某线程,也就是说,在有多个线程的应用程序中,我想得到一个线程(我知道这个线程的实例名),再对其进行操作。
haoxg 2001-07-02
  • 打赏
  • 举报
回复
//---------------------------------------------------------
搜索进程:

unit ProcList;

interface

uses
Windows, Messages, SysUtils, Classes,
Controls, Forms, Dialogs, Consts, TLHelp32;

type
TProcessItems = array of TProcessEntry32;

TProcessList = class
private
FItems: TProcessItems;

function GetItem(Index: Integer): TProcessEntry32;
function GetCount: Integer;
procedure GetProcessList(var Items: TProcessItems);
public
constructor Create;
destructor Destroy; override;

procedure Refresh;
function Terminate(Index: Integer): Boolean;
property Item[Index: Integer]: TProcessEntry32 read GetItem; default;
property Count: Integer read GetCount;
end;

implementation

constructor TProcessList.Create;
begin
inherited Create;
Refresh;
end;

destructor TProcessList.Destroy;
begin
SetLength(FItems, 0);
inherited Destroy;
end;

function TProcessList.GetItem(Index: Integer): TProcessEntry32;
begin
if (Index < 0) or (Index > High(FItems)) then
raise EListError.CreateFmt(SListIndexError, [Index]);
Result := FItems[Index];
end;

function TProcessList.GetCount: Integer;
begin
Result := Length(FItems);
end;

procedure TProcessList.GetProcessList(var Items: TProcessItems);
var
Item: TProcessEntry32;
Ok: Boolean;
ProcessListHandle: THandle;
Count: Integer;
begin
SetLength(FItems, 0);
ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
Item.dwSize := SizeOf(Item);
Count := 0;
Ok := Process32First(ProcessListHandle, Item);
while Integer(Ok) <> 0 do
begin
SetLength(FItems, Count + 1);
FItems[Count] := Item;
Inc(Count);
Ok := Process32Next(ProcessListHandle, Item);
end;
end;

procedure TProcessList.Refresh;
begin
GetProcessList(FItems);
end;

function TProcessList.Terminate(Index: Integer): Boolean;
var
h: THandle;
a: DWORD;
Item: TProcessEntry32;
begin
if (Index < 0) or (Index > High(FItems)) then
raise EListError.CreateFmt(SListIndexError, [Index]);

Item := FItems[Index];
h := OpenProcess(PROCESS_ALL_ACCESS, True, Item.th32ProcessID);
GetExitCodeProcess(h, a);
Result := Integer(TerminateProcess(h, a)) <> 0;
Refresh;
end;

end.

//---------------------------------------------------------
搜索线程:

unit ThrdList;

interface

uses
Windows, Messages, SysUtils, Classes,
Controls, Forms, Dialogs, Consts, TLHelp32;

type
TThreadItems = array of TThreadEntry32;

TThreadList = class
private
FItems: TThreadItems;

function GetItem(Index: Integer): TThreadEntry32;
function GetCount: Integer;
procedure GetThreadList(var Items: TThreadItems);
public
constructor Create;
destructor Destroy; override;

procedure Refresh;
function Terminate(Index: Integer): Boolean;
property Item[Index: Integer]: TThreadEntry32 read GetItem; default;
property Count: Integer read GetCount;
end;

implementation

constructor TThreadList.Create;
begin
inherited Create;
Refresh;
end;

destructor TThreadList.Destroy;
begin
SetLength(FItems, 0);
inherited Destroy;
end;

function TThreadList.GetItem(Index: Integer): TThreadEntry32;
begin
if (Index < 0) or (Index > High(FItems)) then
raise EListError.CreateFmt(SListIndexError, [Index]);
Result := FItems[Index];
end;

function TThreadList.GetCount: Integer;
begin
Result := Length(FItems);
end;

procedure TThreadList.GetThreadList(var Items: TThreadItems);
var
Item: TThreadEntry32;
Ok: Boolean;
ThreadListHandle: THandle;
Count: Integer;
begin
SetLength(FItems, 0);
ThreadListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPTHREAD, 0);
Item.dwSize := SizeOf(Item);
Count := 0;
Ok := Thread32First(ThreadListHandle, Item);
while Integer(Ok) <> 0 do
begin
SetLength(FItems, Count + 1);
FItems[Count] := Item;
Inc(Count);
Ok := Thread32Next(ThreadListHandle, Item);
end;
end;

procedure TThreadList.Refresh;
begin
GetThreadList(FItems);
end;

function TThreadList.Terminate(Index: Integer): Boolean;
begin
Result := False;
end;

end.
ey4s 2001-07-02
  • 打赏
  • 举报
回复
得到本进程外的其他进程就比较麻烦了。
用CreateRemoteThread函数往目标进程插入一个线程,然后这个线程遍历目标进程的线程,得到目标线程的ID,然后用OpenThread函数就可以得到目标线程的伪句柄,然后就可以进行一些操作了。
例子啊,自己在MSDN搜索一下吧。
ey4s 2001-06-30
  • 打赏
  • 举报
回复
得到一个应用程序里的线程数量很简单,一个应用程序就是一个进程,但是不同的系统下实现的方法不尽相同。
在NT/2000下可以调用PSAPI函数来实现,也可以通过PDH来获取,在9x下可以用Tools Help函数,Tools Help也可以在2k下用。
具体的你查一下MSDN吧,上面都有例子。

你说的得到应用程序的线程是指什么?得到句柄?那用OpenThread函数吧。

5,389

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧