你可以使用CreateToolhelp32Snapshot带有TH32CS_SNAPTHREAD标志的功能
看到这段代码.
uses
PsAPI,
TlHelp32,
Windows,
SysUtils;
function GetTThreadsCount(PID:Cardinal): Integer;
var
SnapProcHandle: THandle;
NextProc : Boolean;
TThreadEntry : TThreadEntry32;
Proceed : Boolean;
begin
Result:=0;
SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); //Takes a snapshot of the all threads
Proceed := (SnapProcHandle <> INVALID_HANDLE_VALUE);
if Proceed then
try
TThreadEntry.dwSize := SizeOf(TThreadEntry);
NextProc := Thread32First(SnapProcHandle, TThreadEntry);//get the first Thread
while NextProc do
begin
if TThreadEntry.th32OwnerProcessID = PID then //Check the owner Pid against the PID requested
Inc(Result);
NextProc := Thread32Next(SnapProcHandle, TThreadEntry);//get the Next Thread
end;
finally
CloseHandle(SnapProcHandle);//Close the Handle
end;
end;
Run Code Online (Sandbox Code Playgroud)
并以这种方式调用,使用GetCurrentProcessId函数wich检索应用程序的PID(进程标识符).
Var
Num :integer;
begin
Num:=GetTThreadsCount(GetCurrentProcessId);
end;
Run Code Online (Sandbox Code Playgroud)