Page 1 of 1

Enumerating process threads

Posted: Mon Jul 20, 2009 9:29 pm
by SFSxOI
How do you enumerate all the threads produced by a process and return the thread handle and thread ID ? (not the process handle)

Posted: Tue Jul 21, 2009 12:03 am
by Mistrel

Posted: Tue Jul 21, 2009 1:14 am
by SFSxOI
Thanks Mistrel,

Yep, that was almost kinda what I was looking for. I'll play around with it some and see if I can adapt it to my needs.

Thank You

Just a quick and dirty check, and it looks like....

Code: Select all

Define te.THREADENTRY32

Prototype.i PFNCreateToolhelp32Snapshot(dwFlags.i, th32ProcessID.i) ;
Prototype.b PFNProcess32First(hSnapshot.i, *lppe.PROCESSENTRY32) ;
Prototype.b PFNProcess32Next(hSnapshot.i, *lppe.PROCESSENTRY32) ;
; returns PID 
Procedure GetPidByName(p_name$) 
    Protected hDLL.i, process_name$ 
    Protected PEntry.PROCESSENTRY32, hTool32.i 
    Protected pCreateToolhelp32Snapshot.PFNCreateToolhelp32Snapshot 
    Protected pProcess32First.PFNProcess32First 
    Protected pProcess32Next.PFNProcess32Next 
    Protected pid.i 
    
    hDLL = OpenLibrary(#PB_Any,"kernel32.dll") 
    
    If hDLL 
        pCreateToolhelp32Snapshot = GetFunction(hDLL,"CreateToolhelp32Snapshot") 
        pProcess32First = GetFunction(hDLL,"Process32First") 
        pProcess32Next = GetFunction(hDLL,"Process32Next") 
    Else 
        ProcedureReturn 0 
    EndIf 
    
    PEntry\dwSize = SizeOf(PROCESSENTRY32) 
    hTool32 = pCreateToolhelp32Snapshot(#TH32CS_SNAPPROCESS, 0) 
    pProcess32First(hTool32, @PEntry) 
    process_name$ = Space(#MAX_PATH) 
    CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH) 
    
    If  UCase(process_name$) = UCase(p_name$) 
        ProcedureReturn PEntry\th32ProcessID 
    EndIf 
    
    While pProcess32Next(hTool32, @PEntry) > 0 
        process_name$ = Space(#MAX_PATH) 
        CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH) 
        
        If  UCase(process_name$) = UCase(p_name$) 
            ProcedureReturn PEntry\th32ProcessID 
        EndIf 
    
    Wend 
    
    CloseLibrary(hDLL) 
    
    ProcedureReturn 0 
EndProcedure 

PID.i = GetPidByName("MyProg.exe")

; To identify the threads that belong to a specific process, compare its process identifier to the th32OwnerProcessID member of the THREADENTRY32 structure when enumerating the threads.

If PID > 0
h=CreateToolhelp32Snapshot_(#TH32CS_SNAPTHREAD,0); 
  If (h<>#INVALID_HANDLE_VALUE) 
     te\dwSize=SizeOf(te) 
     If (Thread32First_(h,@te)) 
           Repeat 
           If (te\dwSize>=OffsetOf(THREADENTRY32\th32OwnerProcessID)+SizeOf(te\th32OwnerProcessID))
           
             If PID = te\th32OwnerProcessID
            
              Debug "Process "+RSet(Hex(te\th32OwnerProcessID),4,"0")+" Thread "+RSet(Hex(te\th32ThreadID),4,"0") 
            EndIf
           EndIf
            
           te\dwSize=SizeOf(te); 
        Until Not (Thread32Next_(h,@te)); 
     EndIf 
     CloseHandle_(h); 
  EndIf
EndIf
It works ! :)

Got the thread ID, now to get the thread handle and work with it. And we get the thread handle like this:

Code: Select all


#PROCESS_ALL_ACCESS_VISTA_WIN7 = $1FFFFF

hThread.i = OpenThread_(#PROCESS_ALL_ACCESS_VISTA_WIN7, 0, te\th32ThreadID)

and then I can set the various threads priorities with this:

SetThreadPriority_(hThread,  int_nPriority)

almost all done :)