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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Imthiyaz_phConnect With a Mentor Commented:
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
 
MadshiConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.