techbytnt 发表于 2009-4-12 21:04:37

如何查找进程的线程(How to Get ProcessThreads from A PROCESS)

如何查找到进程的线程,进程里的线程
How to Get ProcessThreads from A PROCESS
谢谢高手解答,小弟感谢不尽.
http://h1.ripway.com/autoit3/processthreads.jpg

[ 本帖最后由 techbytnt 于 2009-4-14 09:38 编辑 ]

pusofalse 发表于 2009-4-12 21:40:45

#include <Array.au3>
#include <WinAPI.au3>

Const $TH32CS_SNAPTHREAD = 4
Const $tagThread = "dword dwSize;dword cntUsage;dword th32ThreadID;" & _
                "dword th32OwnerProcessID;long tpBasePri;" & _
                "long tpDeltaPri;dword dwFlags"


$aThread = _EnumProcessThreads(@AutoItPid)
_ArrayDisplay($aThread)



Func _EnumProcessThreads($iProcessId)
        Local $hSnapshot, $iThreadId, $iOwnerId, $tThread, $aResult

        $iProcessId = ProcessExists($iProcessId)
        $tThread = _Thread32First($hSnapshot)
        $hSnapshot = _CreateToolhelp32Snapshot(0, $TH32CS_SNAPTHREAD)

        While True
                _Thread32Next($hSnapshot, $tThread)
                If @error Then ExitLoop
                $iThreadId = DllStructGetData($tThread, "th32ThreadID")
                $iOwnerId = DllStructGetData($tThread, "th32OwnerProcessID")
                If $iOwnerId = $iProcessId Then
                        $aResult = $iThreadId
                        Redim $aResult
                EndIf
        WEnd
        $tThread = 0
        _WinAPI_CloseHandle($hSnapshot)
        If Ubound($aResult) = 1 Then Return SetError(1, 0, 0)
        Redim $aResult
        Return SetError(0, Ubound($aResult), $aResult)
EndFunc        ;==>_EnumProcessThreads()


Func _CreateToolhelp32Snapshot($iProcessId, $iFlag)
        Local $hSnapshot

        $iProcessId = ProcessExists($iProcessId)
        $hSnapshot = DllCall("Kernel32.Dll", "hWnd", "CreateToolhelp32Snapshot", _
                        "dword", $iFlag, "int", $iProcessId)
        Return $hSnapshot
EndFunc        ;==>_CreateToolhelp32Snapshot()

Func _Thread32First($hSnapshot)
        Local $tThread, $pThread, $iResult

        $tThread = DllStructCreate($tagThread)
        $pThread = DllStructGetPtr($tThread)
        DllStructSetData($tThread, 1, DllStructGetSize($tThread))

        $iResult = DllCall("Kernel32.Dll", "int", "Thread32First", _
                        "hWnd", $hSnapshot, _
                        "ptr", $pThread)
        Return SetError(Not $iResult, 0, $tThread)
EndFunc        ;==>_Thread32First()


Func _Thread32Next($hSnapshot, ByRef $tThread)
        Local $iResult

        $iResult = DllCall("Kernel32.Dll", "int", "Thread32Next", _
                "hWnd", $hSnapshot, _
                "ptr", DllStructGetPtr($tThread))
        Return SetError(Not $iResult, 0, $iResult)
EndFunc        ;==>_Thread32Next()

techbytnt 发表于 2009-4-14 09:37:02

感谢了:face (38):

fireant 发表于 2010-4-20 09:21:40

高手,感谢了

太需要了

liuxuchun1985 发表于 2012-11-22 16:58:12

能返回线程的名称吗?如果有的话
页: [1]
查看完整版本: 如何查找进程的线程(How to Get ProcessThreads from A PROCESS)