Jump to content

ProcessClose() and @error, not ?


Recommended Posts

so @mLipok is asking about @error behavior and I run to answer.
My answers are "put the code where your mouth is", and I like this disposition because is clearer than speech.
 

Global $iError, $iExtended, $iRet = ProcessClose("lol_this_lol.exe")
;~      Return Value
;~      Success: 1.
;~      Failure: 0 and sets the @error flag to non-zero.
;~      @error: 1 = OpenProcess failed
;~      2 = AdjustTokenPrivileges Failed
;~      3 = TerminateProcess Failed
;~      4 = Cannot verify if process exists
;~      @extended: sets to Windows API GetLastError()
$iError = @error
$iExtended = @extended
$iGetLastError = __WinAPI_GetLastError()
$sGetErrorMessage = __WinAPI_GetErrorMessage($iGetLastError)
ConsoleWrite(";  @AutoItVersion = " & @AutoItVersion & @TAB)
ConsoleWrite("  @error = " & $iError & @TAB & "  @extended = " & $iExtended & @TAB & '  iRet = ' & $iRet & @TAB)
ConsoleWrite("  GetLastError = " & $iGetLastError & @TAB & '  GetErrorMessage = "' & $sGetErrorMessage & '"' & @CRLF )

Func __WinAPI_GetLastError(Const $_iCurrentError = @error, Const $_iCurrentExtended = @extended)
    Local $aResult = DllCall("kernel32.dll", "dword", "GetLastError")
    If @error Or Not UBound($aResult) Then  Return SetError($_iCurrentError, $_iCurrentExtended, -1)
    Return SetError($_iCurrentError, $_iCurrentExtended, $aResult[0])
EndFunc   ;==>_WinAPI_GetLastError
Func __WinAPI_GetErrorMessage($iCode, $iLanguage = 0, Const $_iCurrentError = @error, Const $_iCurrentExtended = @extended)
    Local $aRet = DllCall('kernel32.dll', 'dword', 'FormatMessageW', 'dword', 0x1000, 'ptr', 0, 'dword', $iCode, _
            'dword', $iLanguage, 'wstr', '', 'dword', 4096, 'ptr', 0)
    If @error Or Not $aRet[0] Then Return SetError(@error, @extended, '')
    Return SetError($_iCurrentError, $_iCurrentExtended, StringRegExpReplace($aRet[5], '[' & @LF & ',' & @CR & ']*\Z', ''))
EndFunc   ;==>_WinAPI_GetErrorMessage

;  @AutoItVersion = 3.2.0.1       @error = 0      @extended = 0   iRet = 1    GetLastError = -1   GetErrorMessage = ""
;  @AutoItVersion = 3.2.12.1      @error = 0      @extended = 0   iRet = 1    GetLastError = 0    GetErrorMessage = "The operation completed successfully."
;  @AutoItVersion = 3.3.6.1       @error = 0      @extended = 0   iRet = 0    GetLastError = 18   GetErrorMessage = "There are no more files."
;  @AutoItVersion = 3.3.8.1       @error = 0      @extended = 0   iRet = 0    GetLastError = 18   GetErrorMessage = "There are no more files."
;  @AutoItVersion = 3.3.14.5      @error = 0      @extended = 0   iRet = 0    GetLastError = 18   GetErrorMessage = "There are no more files."
;  @AutoItVersion = 3.3.15.3      @error = 0      @extended = 0   iRet = 0    GetLastError = 18   GetErrorMessage = "There are no more files."

and what I find is that there is no error flag or extended flag on failure.
According to the help file, it should return 4, "Cannot verify if process exists", as it in fact, does not exist.
The expectation is "Failure: 0 and sets the @error flag to non-zero.".

Tested in Windows XP, 7 and 10.

Either I need more cafe, or this is ugly wrong and needs attention.

Edit: After @pixelsearch's observation I added those too.

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

@argumentum I'm adding the GetLastError test to your post, after you wrote this sentence :

@extended: sets to Windows API GetLastError()

If we check _WinAPI_GetLastError / _WinAPI_GetLastErrorMessage / _WinAPI_ShowLastError, then it shows :

* error 18 ("there are no more files") when checked just after ProcessClose("lol_this_lol.exe")

* error 0 ("the operation completed successfully") when checked after ProcessClose("notepad.exe") when you got an opened Notepad (which will close). If no Notepad was opened then it returns error 18 too

In your case, this value of 18 should have been passed to @extended, but apparently it's not.
Gladly $iRet is reliable in AutoIt actual versions : 1 = succeed, 0 = fail.

Link to comment
Share on other sites

6 hours ago, jpm said:

in fact checking the code @error= 4 cannot occur

... I don't know the logical order of the error levels, for my assuming of "4", but an @error other than zero should be set. 
Again, as per the help file.
ProcessExists() for example, does not set error level on failure, it would reset @error and @extended to zero regardless of the function return value.
Because a process either exists or not. By the same token, a process got closed or not, so maybe is a help file failure and not a program failure. No clue.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.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...