Link to home
Start Free TrialLog in
Avatar of smartins
smartins

asked on

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
Avatar of ZhaawZ
ZhaawZ
Flag of Latvia image

I'm not sure, but maybe it is GetProcessHandleCount() ?
Avatar of smartins
smartins

ASKER

Acording with MSDE this function requires Windows XP SP1. It's not even declared on delphi PsAPI unit (WinNT process API Interface Unit).

Steven
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.
ASKER CERTIFIED SOLUTION
Avatar of Imthiyaz_ph
Imthiyaz_ph

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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,
oops, it returns the total system thread count, not for a process. sorry.
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?
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial