Code:
var
hThreadSnap: THandle;
te32: tagTHREADENTRY32;
begin
PID:=GetPID(MaskEdit1.Text);
// Take a snapshot of all running threads
hThreadSnap := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap = INVALID_HANDLE_VALUE) then Exit;
// Fill in the size of the structure before using it.
te32.dwSize := SizeOf(THREADENTRY32);
try
if not Thread32First(hThreadSnap, te32) then Exit;
if GetLastError = ERROR_NO_MORE_FILES then Exit;
repeat
if te32.th32OwnerProcessID = PID then // <<< Always 4
begin
ListBox.Items.Add(IntToHex(te32.th32ThreadID, 8)); //'Thread ID: 0x' +
ListBox.Items.Add('base priority: ' + IntToStr(te32.tpBasePri));
ListBox.Items.Add('delta priority: ' + IntToStr(te32.tpDeltaPri));
ListBox.Items.Add(' ');
end;
until not Thread32Next(hThreadSnap, te32);
finally
CloseHandle(hThreadSnap);
end;
end;
or
Code:
var
hSnapshot : THandle;
NextThread : Boolean;
TThreadEntry : TThreadEntry32;
Result : Boolean;
begin
PID:=GetPID(MaskEdit1.Text);
if PID<>0 then
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); //Takes a snapshot of the all threads
Result := (hSnapshot <> INVALID_HANDLE_VALUE);
if Result then
try
TThreadEntry.dwSize := SizeOf(TThreadEntry);
NextThread := Thread32First(hSnapshot, TThreadEntry);//get the first Thread
while NextThread do
begin
if TThreadEntry.th32OwnerProcessID = PID then //Check the owner Pid against the PID requested
ListBox3.Items.Add(Format('Thread Id %.8x Start Address %p',[TThreadEntry.th32ThreadID, GetThreadStartAddress(TThreadEntry.th32ThreadID)]));
NextThread := Thread32Next(hSnapshot, TThreadEntry);//get the Next Thread
end;
finally
CloseHandle(hSnapshot);
end;
end;