VIP

_ProcessCloseFromPath() & _ForceProcessClose()

1 post in this topic

#1 ·  Posted (edited)

 

;~ Opt("TrayAutoPause", 0)
;~ Opt("MustDeclareVars", 1)

; * --------------------:| # Example
Example()

Func Example()
    Run("C:\Windows\System32\notepad.exe")
    Run("C:\Windows\System32\notepad.exe")
    Run("C:\Windows\SysWOW64\notepad.exe")
    Run("C:\Windows\SysWOW64\notepad.exe")
    Local $sProcessPath = "C:\Windows\System32\notepad.exe"
    Local $xProcessPath = "C:\Windows\SysWOW64\notepad.exe"
    Sleep(1000)
    _ProcessCloseByPath($xProcessPath)
    Local $vReturn = _ProcessCloseFromPath($sProcessPath)
    ConsoleWrite("-->Value of @error is: " & @error & @CRLF & "-->Value of @extended is: " & @extended & @CRLF)
    ConsoleWrite("-->Return values: " & $vReturn & @CRLF)
EndFunc   ;==>Example
; * --------------------:| # Example

;~;#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
; #FUNCTION# ====================================================================================================================
; Name...........: _ProcessCloseFromPath()
; Description ...: Close Process from Path
; Syntax.........: _ProcessCloseFromPath($sProcessPath [, $Fc = 1] )
; Parameters ....: $sProcessPath  - Full path of Process
;                  $Fc            - Try Force Close Process
; Return values .: Success = 1 : All ProcessClose OK
;                      @error = 0
;                      @extended = -1 : Process doesn't exist
;                      @extended = Number of Process same name
;                  Failure = 0 : Process Close Failure
;                      @error = -1 : Process File Path Not Exists
;                      @error = Number of Process can't Close
;                      @extended = 0
; Author ........: guinness, Trong
; Example .......: Yes
; ===============================================================================================================================
Func _ProcessCloseFromPath($sProcessPath, $Fc = 1);
    If Not FileExists($sProcessPath) Then Return SetError(-1, 0, 0);
    Local $sProcessName = StringTrimLeft($sProcessPath, StringInStr($sProcessPath, "\", Default, -1));
    If Not ProcessExists($sProcessName) Then Return SetError(0, -1, 1);
    Local $vPID = ProcessList($sProcessName), $n, $sFailure = 0, $Extended = 0, $sSuccess = 1, $sPID
    For $n = 1 To $vPID[0][0];
        $sPID = $vPID[$n][1];
        If (StringLower($sProcessPath) = StringLower(_ProcessGetPath($sPID))) Then;
            If $Fc Then
                ProcessClose($sPID);
            Else
                _ForceProcessClose($sPID);
            EndIf
        Else;
            $Extended = $Extended + 1;
            $sPID = 0;
        EndIf;
    Next;
    For $n = 1 To $vPID[0][0];
        If ProcessExists($sPID) Then $sFailure += 1;
    Next;
    If $sFailure Then $sSuccess = 0;
    Return SetError($sFailure, $Extended, $sSuccess);
EndFunc   ;==>_ProcessCloseFromPath
; * -----:| =====================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _ForceProcessClose()
; Description ...:  Force Process Close
; Syntax.........: _ForceProcessClose($sPID [, $Fc = 10] )
; Parameters ....: $sPID  - PID of Process
;                  $Fc   - Number of try close
; Return values .: Success = 1 : Process Close OK
;                      @error = 0
;                      @extended  = 0
;                      @extended  = -1 : Process doesn't exist
;                  Failure = 0 : Process Close Failure
;                      @error = 1
;                      @extended  = 0
; Modified.......: Trong
; Example .......: Yes
; ===============================================================================================================================
Func _ForceProcessClose($sPID, $Fc = 10);
    If IsString($sPID) Then $sPID = ProcessExists($sPID);
    If Not $sPID Then Return SetError(0, -1, 1);
    While ProcessExists($sPID);
        ProcessClose($sPID);
        Sleep(1);
        $Fc -= 1;
        If $Fc < 1 Then ExitLoop;
    WEnd;
    If ProcessExists($sPID) Then RunWait(@ComSpec & " /c taskkill /F /PID " & $sPID & " /T", @SystemDir, @SW_HIDE);
    If ProcessExists($sPID) Then Return SetError(1, 0, 0);
    Return SetError(0, 0, 1);
EndFunc   ;==>_ForceProcessClose
; * -----:| =====================================================================================================================


; #FUNCTION# ===============================================================================
; Name...........: _ProcessGetPath()
; Description ...: Retrieves a process file path
; Syntax.........: _ProcessGetPath($vProcess)
; Parameters ....: $vProcess - PID or name of a process
; Requirements...: kernel32.dll, psapi.dll
; Return values .: Success - A full process path
;                    @error = 0
;                   Failure - Empty string
;                    @error = 1 - Invalid process name/PID
;                    @error = 2 - kernel32.dll failed to open (wrong version?)
;                    @error = 3 - Could not OpenProcess
;                     @error = 4 - psapi.dll failed to open (doesn't exist?)
;                    @error = 5 - returned path is empty or invalid
; Author ........: JScript, Larry, SmOke_N
; Modified.......: mrRevoked - reformated, error checking : Trong - fix special path
; Example .......: Yes
; ============================================================================================
Func _ProcessGetPath($vProcess)
    Local $i_PID, $hKernel32, $hPsapi, $aProcessHandle, $tDLLStruct, $iError, $sProcessPath
    $i_PID = ProcessExists($vProcess)
    If Not $i_PID Then Return SetError(1, 0, ""); Process doesn't exist?
    $hKernel32 = DllOpen("Kernel32.dll")
    $iError = @error
    If $iError Then
        DllClose($hKernel32)
        Return SetError(2, $iError, ""); DllOpen kernell32.dll failed
    EndIf
    $aProcessHandle = DllCall($hKernel32, "int", "OpenProcess", "int", 0x0400 + 0x0010, "int", 0, "int", $i_PID)
    $iError = @error
    If $iError Or $aProcessHandle[0] = 0 Then
        DllClose($hKernel32)
        Return SetError(2, $iError, ""); OpenPocess failed
    EndIf
    $hPsapi = DllOpen("Psapi.dll")
    $iError = @error
    If $iError Then
        DllClose($hKernel32)
        DllClose($hPsapi)
        Return SetError(3, $iError, ""); DllOpen psapi.dll failed
    EndIf
    $tDLLStruct = DllStructCreate("char[1000]")
    DllCall($hPsapi, "long", "GetModuleFileNameEx", "int", $aProcessHandle[0], "int", 0, "ptr", DllStructGetPtr($tDLLStruct), "long", DllStructGetSize($tDLLStruct))
    $iError = @error
    DllCall($hKernel32, "int", "CloseHandle", "int", $aProcessHandle[0])
    DllClose($hKernel32)
    DllClose($hPsapi)
    If $iError Then
        $tDLLStruct = 0
        Return SetError(4, $iError, "");GetModulefilenamex failed
    EndIf
    $sProcessPath = DllStructGetData($tDLLStruct, 1)
    $tDLLStruct = 0
    ;Format the Output
    If StringLen($sProcessPath) < 2 Then Return SetError(5, 0, "");is empty or non readable
    If StringLeft($sProcessPath, 4) = "\??\" Then $sProcessPath = StringReplace($sProcessPath, "\??\", "")
    If (StringLower(StringLeft($sProcessPath, 20)) = "\systemroot\system32") Then $sProcessPath = StringReplace($sProcessPath, "\SystemRoot\System32", @SystemDir)
    Return SetError(0, 0, $sProcessPath)
EndFunc   ;==>_ProcessGetPath
; * -----:| =====================================================================================================================


; #FUNCTION# ===============================================================================
; Name...........: __ProcessCloseByPath()
; Author ........: boththose
; Modified.......: Trong - reformated
Func _ProcessCloseByPath($sPath);
    Local $colItems = "", $strComputer = "localhost", $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20;
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\ROOT\CIMV2"), $sFailure = 0, $sSuccess = 1;
    Local $cQuery = "SELECT ExecutablePath,ProcessId FROM Win32_Process WHERE ExecutablePath = " & '"' & StringReplace($sPath, "\", "\\") & '"'
    $colItems = $objWMIService.ExecQuery($cQuery, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly);
    If IsObj($colItems) Then;
        For $objItem In $colItems;
            Local $sPID = $objItem.ProcessId;
            ProcessClose($sPID);
            If ProcessExists($sPID) Then $sFailure += 1;
        Next;
    EndIf;
    If $sFailure Then $sSuccess = 0;
    Return SetError($sFailure, 0, $sSuccess);
EndFunc   ;==>_ProcessCloseByPath
; * -----:| =====================================================================================================================

Simple Version:

 

; #FUNCTION# ===============================================================================
; Name...........: _ProcessCloseByPath()
; Author ........: boththose
; Modified.......: Trong - reformated
Func _ProcessCloseByPath($sPath);
    Local $colItems = "", $strComputer = "localhost", $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20;
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\ROOT\CIMV2"), $sFailure = 0, $sSuccess = 1;
    Local $cQuery = "SELECT ExecutablePath,ProcessId FROM Win32_Process WHERE ExecutablePath = " & '"' & StringReplace($sPath, "\", "\\") & '"'
    $colItems = $objWMIService.ExecQuery($cQuery, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly);
    If IsObj($colItems) Then;
        For $objItem In $colItems;
            Local $sPID = $objItem.ProcessId;
            ProcessClose($sPID);
            If ProcessExists($sPID) Then $sFailure += 1;
        Next;
    EndIf;
    If $sFailure Then $sSuccess = 0;
    Return SetError($sFailure, 0, $sSuccess);
EndFunc   ;==>_ProcessCloseByPath
; * -----:| =====================================================================================================================

UDF-3.au3

Edited by Trong

Regards,
 

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