0

I have this code that runs through all threads. I would like to get the threads only from my own process, without having to loop through all the threads running on the system.

var
  SnapProcHandle: THandle;
  NextProc      : Boolean;
  TThreadEntry  : TThreadEntry32;
begin
  SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
  Result := (SnapProcHandle <> INVALID_HANDLE_VALUE);
  if Result then
  try
    TThreadEntry.dwSize := SizeOf(TThreadEntry);
    NextProc := Thread32First(SnapProcHandle, TThreadEntry);
    while NextProc do
    begin
      if TThreadEntry.th32OwnerProcessID = PID then
      begin
        Memo1.Lines.Add('Thread ID      '+IntToStr(TThreadEntry.th32ThreadID));
        Memo1.Lines.Add('base priority  '+inttostr(TThreadEntry.tpBasePri));
        Memo1.Lines.Add('delta priority '+inttostr(TThreadEntry.tpBasePri));
      end;
      NextProc := Thread32Next(SnapProcHandle, TThreadEntry);
    end;
  finally
    CloseHandle(SnapProcHandle);
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mutha
  • 11
  • 1
  • Your English is fine but your code is unreadable. Edit the question, read the help on editing a a format the code. Spend some time doing this. Don't give up after 20s like so many before you. – David Heffernan Jul 27 '16 at 20:19
  • dupes: http://stackoverflow.com/q/2055642/800214 and http://stackoverflow.com/questions/3809708/running-threads-count – whosrdaddy Jul 27 '16 at 20:23
  • It is also difficult to understand the question and how the code relates to it. What do you expect the code to do. What does it do. – David Heffernan Jul 27 '16 at 20:25
  • He wants to avoid the loop? seems clear to me... – whosrdaddy Jul 27 '16 at 20:26
  • Hard to believe that could be the case. The code performs the task correctly but asker wants a different solution that performs the exact same task? Why would anyone want that? – David Heffernan Jul 27 '16 at 20:27
  • 1
    Anyway, code is from [RRUZ's answer](http://stackoverflow.com/a/2057866/800214) – whosrdaddy Jul 27 '16 at 20:28
  • I do not understand, the code looks fine readable here. The aim would be to avoid the loop as @ whosrdaddy mensionou. – Mutha Jul 27 '16 at 20:39
  • The question has already been edited. Denying you an chance to learn how to do it. Next time you ask the same will happen again. Why do you want to avoid the loop. Your code works. Why not make a feature request to MS to provide a new interface for you to use. Since you don't like the one they gave you that works. – David Heffernan Jul 27 '16 at 20:41
  • I see no need to get information out of my context. Maybe podeira be a little more concise that work. But if this is the best way I will use this code yourself. – Mutha Jul 27 '16 at 21:00
  • RRUZ's answer has the same *typo* and mistake about the delta priority. It's trDeltaPri (typo), and it's not used (mistake). Do not blindly copy code, try to understand before you use. – Sertac Akyuz Jul 27 '16 at 22:28
  • I corrected the mistake. – Remy Lebeau Jul 27 '16 at 23:32

2 Answers2

2

You already know how to filter the threads for a specific process, because the code is already doing exactly that:

if TThreadEntry.th32OwnerProcessID = PID then

All you need is the PID for the calling process. Use GetCurrentProcessId() to get that value.

Unfortunately, CreateToolhelp32Snapshot() does not allow you to restrict the snapshot to a specific process when using TH32CS_SNAPTHREAD, the snapshot includes all threads in the system, so you need to filter them on their respective PIDs as you loop through them.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

You can do this with WMI (Windows MIcrosoft Instrumentation). Here there are an article that explain how to retrieve all threads running of one process. The article is in spanish, but you can use authomatic translation o view the code and download samples.

Using WMI you can obtain all information of a process using the class Win32_Process class. You can try execute in a console a coomand like this, to obtain information of this class.

WMIC Process where name="bds.exe" GET Name, description, ProcessId, ThreadCount, Handle

enter image description here

With this you can obtain information of process.

The second step if "How to retrieve the Threads associated a process". You can do this with the Win32_Thread class.

If you launch a query like this:

SELECT * FROM WIN32_THREAD WHERE ProcessHandle=10740

You get all the threads of the process 10740 (see ProcessId of the first query).

Regards.

Community
  • 1
  • 1
  • Good alternative, liked the lib and therefore accept the answer. – Mutha Jul 29 '16 at 18:26
  • @Mutha - This doesn't avoid retrieving all threads as evident from the sql. Only, it's not your code which is looping but WMI. – Sertac Akyuz Jul 30 '16 at 16:06
  • The provided Spanish article links to a 404. Here's a link to the [translated archived page on Wayback Machine](https://translate.google.com/translate?sl=auto&tl=en&u=https://web.archive.org/web/20181207173953/http://neftali.clubdelphi.com/procesos-y-threads-glibwmi/) – sp00n Mar 27 '21 at 15:04