Get number of threads used for each process

I have a process ID, and I'm able to get the memory used and priority for each one but I can't figure out how to get all the threads used for each process.

Anyone knows how to get the number of threads used for each process, both on Win9X and Windows NT/2000/XP?

Thanks,
Steven
smartinsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZhaawZSoftware DeveloperCommented:
I'm not sure, but maybe it is GetProcessHandleCount() ?
0
smartinsAuthor Commented:
Acording with MSDE this function requires Windows XP SP1. It's not even declared on delphi PsAPI unit (WinNT process API Interface Unit).

Steven
0
Imthiyaz_phCommented:
try using the CreateToolhelp32Snapshot  API to get a snapshot of all running processes.
you can then iterate thru the process list using Process32First and Process32Next. Both these method returns complete information about the process in  PROCESSENTRY32 structure.

typedef struct tagPROCESSENTRY32 {
    DWORD dwSize;
    DWORD cntUsage;
    DWORD th32ProcessID;
    DWORD th32DefaultHeapID;
    DWORD th32ModuleID;
    DWORD cntThreads;             ////////// no. of threads
    DWORD th32ParentProcessID;
    LONG  pcPriClassBase;
    DWORD dwFlags;
    char szExeFile[MAX_PATH];
} PROCESSENTRY32;

you can find the translation of these APIs in TlHelp32 unit.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Imthiyaz_phCommented:
This function will help you:

function ThreadCount(iProcessID: DWord): integer;
var
  SnapHandle: THandle;
  ProcEntry: TProcessEntry32;
begin
  Result := 0;

  SnapHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if SnapHandle = 0 then Exit;

  FillChar(ProcEntry, SizeOf(TProcessEntry32), 0);
  ProcEntry.dwSize := SizeOf(TProcessEntry32);

  if Process32First(SnapHandle, ProcEntry) then
  begin
    if ProcEntry.th32ProcessID = iProcessID then
    begin
      Result := ProcEntry.cntThreads;
      Exit;
    end;

    while Process32Next(SnapHandle, ProcEntry) = true do
    begin
      if ProcEntry.th32ProcessID = iProcessID then
      begin
        Result := ProcEntry.cntThreads;
        Break;
      end;
    end;
  end;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smartinsAuthor Commented:
Imthiyaz_ph,

I'm using the CreateToolhelp32Snapshot API to get a snapshot of all running processes but only for Windows 9x/ME.

For Windows NT4/2000/XP I'm using the EnumProcesses function. Something like this:

  Procedure BuildListPS;
  var
    PMC: PROCESS_MEMORY_COUNTERS;
    Handle: THandle;
    PIDs: array[0..1024] of DWORD;
    Needed: DWORD;
    ModuleFileName: array[0..MAX_PATH] of Char;
    IB: Integer;
  begin
    if not EnumProcesses(@PIDs, Sizeof(PIDs), Needed) then Exit;
    for IB := 0 to (Needed div Sizeof(DWORD)) - 1 do
      if PIDs[IB] <> 0 then
      begin
        Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PIDs[IB]);
        if Handle <> 0 then
          begin
            GetProcessMemoryInfo(Handle, @PMC, SizeOf(PROCESS_MEMORY_COUNTERS));
            Filename := ModuleFileName;
            //.....
          end;
        CloseHandle(Handle);
      end;
  end;

Any ideas on how to get the thread count for each process with this aproach?

Steven
0
Imthiyaz_phCommented:
In MSDN, its says to use the GetPerformanceInfo method of PSAPI, but i coudnt find the implementation of this method in Delphi's psapi.pas.

It returns a record which contains the thread count.

typedef struct _PERFORMANCE_INFORMATION {
  DWORD cb;
  SIZE_T CommitTotal;
  SIZE_T CommitLimit;
  SIZE_T CommitPeak;
  SIZE_T PhysicalTotal;
  SIZE_T PhysicalAvailable;
  SIZE_T SystemCache;
  SIZE_T KernelTotal;
  SIZE_T KernelPaged;
  SIZE_T KernelNonpaged;
  SIZE_T PageSize;
  DWORD HandleCount;
  DWORD ProcessCount;
  DWORD ThreadCount; /////////////////////////
} PERFORMANCE_INFORMATION,
0
Imthiyaz_phCommented:
oops, it returns the total system thread count, not for a process. sorry.
0
MadshiCommented:
The toolhelp functions work for all win9x OSs and for w2k and newer NT OSs. They don't work on NT4, though. Do you need NT4 support, too?
0
smartinsAuthor Commented:
Unfortunately I do need support for NT4 :/

I tried using the CreateToolhelp32Snapshot to get the information about a single process (knowing the handle), but the only way I could find out of getting the TProcessEntry32 values filled are Process32First and Process32Next.

Any way to use the EnumProcesses to get all processes running and then use CreateToolhelp32Snapshot to get individual information about each process retrieved by EnumProcesses?

Thanks,
Steven
0
MadshiCommented:
If you need to support NT4, too, then the toolhelp functions alone won't help. Enumerating threads in NT4 is possible by using the undocumented API NtQuerySystemInformation. Alternatively you can use the performance counters somehow, but that's even more complicated than using NtQuerySystemInformation.

Try this one:

http://madshi.net/enumStuff.zip

It does what you need, it's free and comes with sources. It works only with Delphi 4 or newer versions, though.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.