Sign in to follow this  
Followers 0
rasim

Kill process with used WinAPI

3 posts in this topic

How can terminate process with used WinAPI? I tried, but unsuccessful. :)

Process_Kill("totalcmd.exe")

Func Process_Kill($sProc)
    Local $ProcHandle, $StrPoint
    Local $ProcessPid = ProcessExists($sProc)
    If $ProcessPid = 0 Then
        MsgBox(16, "Error", "Process " & $sProc & " not exist")
        Return SetError(1)
    EndIf
    
    $ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", BitOR(0x0400, 0x0004), "int", 0, "dword", $ProcessPid)
    
    If $ProcHandle[0] = 0 Then
        MsgBox(16, "Error", "Process " & $sProc & " not exist")
        Return SetError(1)
    EndIf
    
    $ProcHandle = $ProcHandle[0]
    
    Local $pStruct = DllStructCreate("dword", $ProcHandle)
    
    $ProcExitCode = DllCall("kernel32.dll", "int", "GetExitCodeProcess", "hwnd", $ProcHandle, "dword*", DllStructGetPtr($pStruct))
    
    $ProcTerminate = DllCall("kernel32.dll", "int", "TerminateProcess", "hwnd", $ProcHandle, "uint", $ProcExitCode[0])
    
    $CloseHandle = DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $ProcHandle)
    ConsoleWrite("@@ (6) : " & $CloseHandle[0] & "       ")
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Not entirely sure why you are trying to do this. The problem was that you didn't open the process with termination permission.

Dim $sProcess = "notepad.exe"
Process_Kill($sProcess)
If @error Then
    Switch @error
        Case 1
            MsgBox(16, "Error", "The process " & $sProcess & " does not exist")
        Case 2
            MsgBox(16, "Error", "Unable to open " & $sProcess)
        Case 3
            MsgBox(16, "Error", "Unable to get the exit code for " & $sProcess)
        Case 4
            MsgBox(16, "Error", "Unable to close the open handle for " & $sProcess)
        Case Else
            Msgbox(16, "Error", "Unknown error.")
    EndSwitch
Else
    Msgbox(64,"Success","The function returned without error and the process should now no longer exist.")
EndIf


Func Process_Kill($sProc)
    Local $ProcHandle, $StrPoint
    Local $ProcessPid = ProcessExists($sProc)
    If $ProcessPid = 0 Then
        Return SetError(1)
    EndIf

    $ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", BitOR(0x0400,0x0004,0x0001), "int", 0, "dword", $ProcessPid)

    If UBound($ProcHandle) > 0 Then
        ConsoleWrite("->OpenProcess returned:"&$ProcHandle[0]&@LF)
        If $ProcHandle[0] = 0 Then Return SetError(2)
    Else
        ConsoleWrite("!>Error with OpenProcess"&@LF)
        Return SetError(2)
    EndIf

    $ProcHandle = $ProcHandle[0]

    Local $pStruct = DllStructCreate("dword")

    $ProcExitCode = DllCall("kernel32.dll", "int", "GetExitCodeProcess", "hwnd", $ProcHandle, "int_ptr", DllStructGetPtr($pStruct))
    If Ubound($ProcExitCode) > 0 Then
        ConsoleWrite("->GetExitCodeProcess returned:"&$ProcExitCode[0]&@LF)
    Else
        ConsoleWrite("!>Error with GetExitCodeProces"&@LF)
        $CloseHandle = DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $ProcHandle)
        Return SetError(3)
    EndIf

    $ProcTerminate = DllCall("kernel32.dll", "int", "TerminateProcess", "hwnd", $ProcHandle, "uint",DllStructGetData($pStruct,1))
    If UBound($ProcTerminate) > 0 Then
        ConsoleWrite("->Terminate Process returned:"& $ProcTerminate[0]&@LF)
    Else
        ConsoleWrite("!>Error with TerminateProcess."&@LF)
    EndIf

    $CloseHandle = DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $ProcHandle)
    If Ubound($CloseHandle) > 0 Then
        ConsoleWrite("->CloseHandle returned:"&$CloseHandle[0]&@LF)
    Else
        ConsoleWrite("!>Error with CloseHandle"&@LF)
        Return SetError(4)
    EndIf
    Return 1
EndFunc

Didn't test everything out, but that killed the process for me and successfully closed the process handle.

- The Kandie Man ;-)

EDIT: Added more console debugging information to the code. Also added return error values to the function and placed the error messageboxes in a switch outside the function.

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The Candie Man

Thanks man!

The problem was that you didn't open the process with termination permission.

My error is 0x0004, i take this value from FAQ in one russian site, but this value does not exists in MSDN library.

Now all work fine!

Added more console debugging information to the code. Also added return error values to the function and placed the error messageboxes in a switch outside the function.

Thanks for your FAQ! :) Edited by rasim

Share this post


Link to post
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
Sign in to follow this  
Followers 0