关于进程和线程的问题,小弟不解 问题如下:1.怎么枚举一个进程中所有线程的ID.2.怎样由一个线程的ID得到它的父进程的句柄.第一个问题就如:SPY++的Process选项,就就能达到这种效果.这两个问题,小弟查阅了很多API不过没有什么好的办法,望大虾赐教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 >>2.怎样由一个线程的ID得到它的父进程的句柄.看是不是你要的void GetProcessIdOfThread( HANDLE Thread); GetProcessIdOfThread 这是个什么函数,没看见过啊.MSDN上也查不到! 刚才只顾写东西,我记我这里好象有代码,翻了一下得知用CreateTollHelp32Snapshot创建信息快照,Thread32First获取第一个线程Thread32Next获取剩下的线程,代码如下,兄弟看一下,测试通过。unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, clipbrd, tlhelp32;type TForm1 = class(TForm) Button1: TButton; ListBox1: TListBox; ComboBox1: TComboBox; Label1: TLabel; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } FSnapshotHandle: THandle; function GetProcessID(var List: TStringList; FileName: string = ''): TProcessEntry32; end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);var FProcessEntry32: TProcessEntry32; ProcessID: DWord; ThreadHandle: THandle; ThreadStruct: TThreadEntry32; List: TStringList;begin Listbox1.items.clear; if Combobox1.itemindex = -1 then exit; List := TStringList.Create; FProcessEntry32 := GetProcessID(List, Combobox1.text); ProcessID := FProcessEntry32.th32ProcessID; ThreadHandle := CreateToolHelp32Snapshot(TH32CS_SnapThread, Processid); try ThreadStruct.dwSize := sizeOf(TThreadEntry32); if Thread32First(ThreadHandle, ThreadStruct) then repeat if ThreadStruct.th32OwnerProcessID = ProcessID then Listbox1.Items.add(IntTostr(ThreadStruct.th32ThreadID)); until not Thread32Next(ThreadHandle, ThreadStruct); finally CloseHandle(ThreadHandle) end; Label1.Caption := '线程数:' + IntTostr(Listbox1.Items.count);end;function TForm1.GetProcessID(var List: TStringList; FileName: string = ''): TProcessEntry32;var Ret: BOOL; ProcessID: integer; s: string; FProcessEntry32: TProcessEntry32;begin FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); FProcessEntry32.dwSize := Sizeof(FProcessEntry32); Ret := Process32First(FSnapshotHandle, FProcessEntry32); while Ret do begin s := ExtractFileName(FProcessEntry32.szExeFile); if (FileName = '') then begin List.Add(Pchar(s)); end else if (AnsicompareText(Trim(s),Trim(FileName))=0) and (FileName <> '') then begin ProcessID := FProcessEntry32.th32ProcessID; List.Add(Pchar(s)); result := FProcessEntry32; break; end; Ret := Process32Next(FSnapshotHandle, FProcessEntry32); end; CloseHandle(FSnapshotHandle);end;procedure TForm1.FormCreate(Sender: TObject);var List: TStringList; i: integer;begin Combobox1.clear; List := TStringList.Create; GetProcessID(List); for i := 0 to List.Count - 1 do begin Combobox1.items.add(Trim(List.strings[i])); end; List.Free; Combobox1.itemindex := 0;end;end. 楼上的代码应该可以实现.. 不过 ToolHelp 系列函数只有在NT 2K才能用这方面的代码应该有见过 待会去找找 如何把C++的 WCHAR bString[1];转换成delphi的?定义是什么 如何实现dbgrid的排序,又经验的朋友指点一下啊 关于在Access中查询的SQL delphi中关于修改记录日期的问题 SQL语句改错 用DELPHI程序可以实现检测计算机的USB接口是2.0还是1.1吗? access数据库删除问题 做搜索文件时碰到的问题_______在线等待,高分相送 有关DCC32的问题 都来看看吧,有 最后一点分全部散了。两个问题: 求avi转bmp和bmp转avi的控件,非常着急,在线等待
看是不是你要的void GetProcessIdOfThread(
HANDLE Thread
);
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, clipbrd, tlhelp32;
type TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ComboBox1: TComboBox;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FSnapshotHandle: THandle;
function GetProcessID(var List: TStringList; FileName: string = ''): TProcessEntry32;
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
FProcessEntry32: TProcessEntry32;
ProcessID: DWord;
ThreadHandle: THandle;
ThreadStruct: TThreadEntry32;
List: TStringList;
begin
Listbox1.items.clear;
if Combobox1.itemindex = -1 then exit;
List := TStringList.Create;
FProcessEntry32 := GetProcessID(List, Combobox1.text);
ProcessID := FProcessEntry32.th32ProcessID;
ThreadHandle := CreateToolHelp32Snapshot(TH32CS_SnapThread, Processid);
try
ThreadStruct.dwSize := sizeOf(TThreadEntry32);
if Thread32First(ThreadHandle, ThreadStruct) then
repeat
if ThreadStruct.th32OwnerProcessID = ProcessID then
Listbox1.Items.add(IntTostr(ThreadStruct.th32ThreadID));
until not Thread32Next(ThreadHandle, ThreadStruct);
finally
CloseHandle(ThreadHandle)
end;
Label1.Caption := '线程数:' + IntTostr(Listbox1.Items.count);
end;function TForm1.GetProcessID(var List: TStringList; FileName: string = ''): TProcessEntry32;
var
Ret: BOOL;
ProcessID: integer;
s: string;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
Ret := Process32First(FSnapshotHandle, FProcessEntry32);
while Ret do
begin
s := ExtractFileName(FProcessEntry32.szExeFile);
if (FileName = '') then
begin
List.Add(Pchar(s));
end else if (AnsicompareText(Trim(s),Trim(FileName))=0) and (FileName <> '') then
begin
ProcessID := FProcessEntry32.th32ProcessID;
List.Add(Pchar(s));
result := FProcessEntry32;
break;
end;
Ret := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
List: TStringList;
i: integer;
begin
Combobox1.clear;
List := TStringList.Create;
GetProcessID(List);
for i := 0 to List.Count - 1 do
begin
Combobox1.items.add(Trim(List.strings[i]));
end;
List.Free;
Combobox1.itemindex := 0;
end;end.