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;