How to get PID of last created thread in process

delphi package - getting into other processes
Post Reply
Posts: 1
Joined: Tue Apr 14, 2009 10:03 pm

How to get PID of last created thread in process

Post 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 ;
   ThreadEntry : ThreadEntry32;
  GotThread : Boolean;

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

  if GotThread and (ThreadEntry.th32OwnerProcessID <> ProcessID) then
     GotThread := Thread32Next(Handle,ThreadEntry);
    until (not GotThread) or (ThreadEntry.th32OwnerProcessID = ProcessID ) ;

    if GotThread
      Result := ThreadEntry.th32ThreadID
    else Result := 0;
but how to get pid of last thread :(
Site Admin
Posts: 10527
Joined: Sun Mar 21, 2004 5:25 pm

Post 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!
Site Admin
Posts: 1027
Joined: Wed Jun 08, 2005 5:08 am

Post 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.

Post Reply