Page 1 of 1

How to get PID of last created thread in process

Posted: Tue Apr 14, 2009 10:08 pm
by beBoss
How to get PID of last created thread in process ?

i use function to get first :

Code: Select all

function GetThreadID(ProcessID : DWORD ) : DWORD ;
var
    Handle:tHandle;
   ThreadEntry : ThreadEntry32;
  GotThread : Boolean;
begin

  Handle:=CreateToolHelp32SnapShot(TH32CS_SNAPTHREAD ,0);
  ThreadEntry.dwSize:=SizeOf(ThreadEntry);
  GotThread := Thread32First(Handle, ThreadEntry);

{$B-}
  if GotThread and (ThreadEntry.th32OwnerProcessID <> ProcessID) then
    repeat
     GotThread := Thread32Next(Handle,ThreadEntry);
    until (not GotThread) or (ThreadEntry.th32OwnerProcessID = ProcessID ) ;

{$B+}
    if GotThread
    then
      Result := ThreadEntry.th32ThreadID
    else Result := 0;
      CloseHandle(Handle);
end;
but how to get pid of last thread :(

Posted: Thu Apr 23, 2009 12:08 pm
by madshi
I wouldn't know how to find out for sure which was the "last created thread" in a process. Of course you could try enumerating all threads and hope that the last one listed would also be the last one created. But I wouldn't bet on that it really is. I'm not even sure if Windows itself keeps a record of which thread was created at which time!

Posted: Wed May 27, 2009 11:12 pm
by iconic
You can't judge the "last created" thread by merely its thread id (TID), the only way that I know of is to compare thread creation times via GetThreadTimes(), it will fill a FILETIME struct with the creation time of the thread. Enumerate all threads in a process, like you're doing, and compare all thread creation times (param 2 of GetThreadTimes) while looking for the highest time, that will be your last created thread without a doubt in a given process.

--Iconic