Jump to content

Kill all Threads in process


Recommended Posts

This is a code that kills all threads on a given process one by one (kills a process even if the process can't be killed the usual method)

Edit Note: Original problem fixed

$PIDtoKill=ProcessExists ('calc.exe')

$objWMIService = ObjGet("winmgmts:\root\cimv2")
$procToKill=$objWMIService.ExecQuery ("Select * from win32_process where processid = "&$PIDtoKill)

For $procToKillThread In $procToKill
    ConsoleWrite($procToKillThread.Terminate&@CRLF)
Next
Edited by dexto
Link to comment
Share on other sites

While i'm at it here is my new function example:

for $i=1 to 5
    run(@SystemDir&'\calc.exe')
Next
msgbox(0,'ProcessKill()',"Now lets kill all of them")

Func ProcessKill($ProcessKill_in);PID or the name of the process
    If IsNumber($ProcessKill_in) Then
        $ProcessKill_PID = $ProcessKill_in
    Else
        $ProcessKill_all = ProcessList($ProcessKill_in)
        If @error Then Return 0

        $ProcessKill_trackEach = 0
        For $ProcessKill_each = 1 To $ProcessKill_all[0][0]
            $ProcessKill_trackEach += ProcessKill($ProcessKill_all[$ProcessKill_each][1])
        Next

        If $ProcessKill_trackEach == $ProcessKill_all[0][0] Then
            Return 1
        Else
            Return 0
        EndIf
    EndIf

    $ProcessKill_PIDtoKill = ProcessExists($ProcessKill_PID)
    If $ProcessKill_PIDtoKill == 0 Then
        Return 0
    EndIf

    ProcessClose($ProcessKill_PIDtoKill)
    Sleep(300)
    If ProcessExists($ProcessKill_PIDtoKill) == 0 Then
        Return 1
    EndIf

    $ProcessKill_objWMIService = ObjGet("winmgmts:\root\cimv2")
    $ProcessKill_procToKill = $ProcessKill_objWMIService.ExecQuery("Select * from win32_process where processid = " & $ProcessKill_PIDtoKill)

    $ProcessKill_track = 0
    For $ProcessKill_procToKillThread In $ProcessKill_procToKill
        $ProcessKill_track += ConsoleWrite($ProcessKill_procToKillThread.Terminate & @CRLF)
    Next

    If $ProcessKill_track > 0 Then
        Return 0
    EndIf
    Return 1
EndFunc  ;==>ProcessKill

ConsoleWrite(processkill('calc.exe')&@CRLF)
Link to comment
Share on other sites

Nice functional code dude, but you really should have posted this in the example scripts forum....

unless you have some problem with it???

oh i see u now... nice post bump exercise. *yawn*

Edited by Mobius

wtfpl-badge-1.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...