Code : Tout sélectionner
;===============================================================================
;
; Description: Suspend all Threads in a Process
; Parameter(s): $vProcess - Name or PID of Process
; Requirement(s): 3.1.1.130, Win ME/2k/XP
; Return Value(s): On Success - Returns Nr. of Threads Suspended and Set @extended to Nr. of Threads Processed
; On Failure - Returns False and Set
; @error to: 1 - Process not Found
; 2 - Error Calling 'CreateToolhelp32Snapshot'
; 3 - Error Calling 'Thread32First'
; 4 - Error Calling 'Thread32Next'
; 5 - Not all Threads Processed
; Author(s): Florian 'Piccaso' Fida
; Note(s): Ported from: http://www.codeproject.com/threads/pausep.asp
; Better read the article (and the warnings!) if you want to use it :)
;
;===============================================================================
Func _ProcSuspend($vProcess, $iReserved = 0)
Local $iPid, $vTmp, $hThreadSnap, $ThreadEntry32, $iThreadID, $hThread, $iThreadCnt, $iThreadCntSuccess, $sFunction
Local $TH32CS_SNAPTHREAD = 0x00000004
Local $INVALID_HANDLE_VALUE = 0xFFFFFFFF
Local $THREAD_SUSPEND_RESUME = 0x0002
Local $THREADENTRY32_StructDef = _
"int;" _ ; 1 -> dwSize
& "int;" _ ; 2 -> cntUsage
& "int;" _ ; 3 -> th32ThreadID
& "int;" _ ; 4 -> th32OwnerProcessID
& "int;" _ ; 5 -> tpBasePri
& "int;" _ ; 6 -> tpDeltaPri
& "int" ; 7 -> dwFlags
$iPid = ProcessExists($vProcess)
If Not $iPid Then Return SetError(1, 0, False) ; Process not found.
Local $DLL = DllOpen("kernel32.dll")
$vTmp = DllCall($DLL, "ptr", "CreateToolhelp32Snapshot", "int", $TH32CS_SNAPTHREAD, "int", 0)
If @error Then
DllClose($DLL)
Return SetError(2, 0, False) ; CreateToolhelp32Snapshot Failed
EndIf
If $vTmp[0] = $INVALID_HANDLE_VALUE Then
DllClose($DLL)
Return SetError(2, 0, False) ; CreateToolhelp32Snapshot Failed
EndIf
$hThreadSnap = $vTmp[0]
$ThreadEntry32 = DllStructCreate($THREADENTRY32_StructDef)
DllStructSetData($ThreadEntry32, 1, DllStructGetSize($ThreadEntry32))
$vTmp = DllCall($DLL, "int", "Thread32First", "ptr", $hThreadSnap, "long", DllStructGetPtr($ThreadEntry32))
If @error Then
DllClose($DLL)
Return SetError(3, 0, False) ; Thread32First Failed
EndIf
If Not $vTmp[0] Then
DllCall($DLL, "int", "CloseHandle", "ptr", $hThreadSnap)
DllClose($DLL)
Return SetError(3, 0, False) ; Thread32First Failed
EndIf
While 1
If DllStructGetData($ThreadEntry32, 4) = $iPid Then
$iThreadID = DllStructGetData($ThreadEntry32, 3)
$vTmp = DllCall($DLL, "ptr", "OpenThread", "int", $THREAD_SUSPEND_RESUME, "int", False, "int", $iThreadID)
If Not @error Then
$hThread = $vTmp[0]
If $hThread Then
If $iReserved Then
$sFunction = "ResumeThread"
Else
$sFunction = "SuspendThread"
EndIf
$vTmp = DllCall($DLL, "int", $sFunction, "ptr", $hThread)
If $vTmp[0] <> -1 Then $iThreadCntSuccess += 1
DllCall($DLL, "int", "CloseHandle", "ptr", $hThread)
EndIf
EndIf
$iThreadCnt += 1
EndIf
$vTmp = DllCall("kernel32", "int", "Thread32Next", "ptr", $hThreadSnap, "long", DllStructGetPtr($ThreadEntry32))
If @error Then
DllClose($DLL)
Return SetError(4, 0, False) ; Thread32Next Failed
EndIf
If Not $vTmp[0] Then ExitLoop
WEnd
DllCall($DLL, "int", "CloseToolhelp32Snapshot", "ptr", $hThreadSnap) ; CloseHandle
If Not $iThreadCntSuccess Or $iThreadCnt > $iThreadCntSuccess Then Return SetError(5, $iThreadCnt, $iThreadCntSuccess)
DllClose($DLL)
Return SetError(0, $iThreadCnt, $iThreadCntSuccess)
EndFunc ;==>_ProcSuspend