Results 1 to 8 of 8

Thread: How to enumerate all threads in a process?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Question How to enumerate all threads in a process?

    Maybe someone can show me how to enumerate all threads in a process?
    I know that I should use CreateToolhelp32Snapshot, Process32First, Process32Next and I have made some attempts, but they was unsuccessful. So maybe someone could showe me how to do that correctly?

  2. #2
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to enumerate all threads in a process?

    No, that wont do!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to enumerate all threads in a process?

    Quote Originally Posted by Dungeon Keeper View Post
    Yes, I saw that before, but it doesn't help me much because I don't understand some parts of that code (it is not VB).

  4. #4
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to enumerate all threads in a process?

    What exactly you don't understand there, ill try to translate it
    No, that wont do!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to enumerate all threads in a process?

    Quote Originally Posted by Dungeon Keeper View Post
    What exactly you don't understand there, ill try to translate it
    I don't know, this code is missing some things, for example flags for CreateToolhelp32Snapshot and so on. Even if you'll translate this code, it will still be missing huge amount of code and other declarations. So I think that in this case, it is much more clever to write new code in VB from zero.

  6. #6
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to enumerate all threads in a process?

    In any case you will need to declare those constants, structures and API functions to make it work in VB.

    Here are some declarations:
    Code:
    Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.dll" ( _ 
    	 ByVal dwFlags As Long, _ 
    	 ByVal th32ProcessID As Long) As Long 
    
    Declare Function CloseHandle Lib "kernel32.dll" ( _ 
    	 ByVal hObject As Long) As Long 
    
    Const TH32CS_SNAPTHREAD As Long = &H4
    
    Const INVALID_HANDLE_VALUE As Long = ((Handle) - 1) 'I think this indicates a function failure so you can use 0 for it.
    You still need to find declarations for:
    FIELD_OFFSET
    THREADENTRY32

    I have tested it and can guarantee that this code works, it shows process IDs and all thread IDs associated with that process ID, so it does what you need. You will only need to find the process EXE name if you want that too in your process/thread list.

    I'm not sure about threads, but i doubt that threads have names - only IDs.
    No, that wont do!

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to enumerate all threads in a process?

    I already have all needed declarations and constants. I just need help with code construction.

  8. #8
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to enumerate all threads in a process?

    Then code would be:


    Code:
    Dim h As HANDLE 'or it maybe could be Long
    h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)
    IF h <> INVALID_HANDLE_VALUE THEN
    Dim te As THREADENTRY32
    te.dwSize = LenB(te)
    if Thread32First(h, &te) THEN
        DO
            IF te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + LenB(te.th32OwnerProcessID)) THEN
               MsgBox "Process " & te.th32OwnerProcessID & "Thread "  & te.th32ThreadID
            END IF
            te.dwSize = LenB(te)
        LOOP WHILE (Thread32Next(h, &te))
    CloseHandle(h)
    No, that wont do!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width