Jump to content

Error exception


akorx
 Share

Recommended Posts

Hi guys,

Sorry for my bad englsih i'm french :sweating: .

I've got a script that kill notepad.exe if the owner that has launched it is the user that has opened the windows session :

#NoTrayIcon
;#include <Array.au3>; Only for _ArrayDisplay()


While 1
    Sleep(10)
    TestProcess()
WEnd


Func TestProcess()
    $AllProcesses = ProcessList()
    For $i = 1 To $AllProcesses[0][0]
        If StringInStr($AllProcesses[$i][0], "notepad.exe") And StringInStr(_ProcessGetOwner($AllProcesses[$i][1], "."), @UserName) Then ProcessClose($AllProcesses[$i][0])
    Next
EndFunc   ;==>TestProcess

;===============================================================================
;
; Function Name:    _ProcessGetOwner()
; Description:      Get the owner of an open process
; Parameter(s):     $vProcess - PID of a process.
; $strComputer - Name of target computer
; Requirement(s):   AutoIt Beta v3.2.+
;                   cimwin32.dll (included with Windows)
; Return Value(s):  On Success - Returns owners username of process
;                     0 - Successful, returns username
;
; On Failure:       Returns friendly errormessage and sets @Error to:
;                     2 - Access denied
;                     3 - Insufficient privilege
;                     8 - Unknown failure
;                     21 - Path not found

; Author(s):        Andreas Fälldin
;===============================================================================

Func _ProcessGetOwner($vProcess, $strComputer)

    Local $i_PID = ProcessExists($vProcess)
    If Not $i_PID Then
        SetError(1)
        Return -1
    EndIf

    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $colItems = ""
    Local $strUser = ""

    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE ProcessID = " & $i_PID, "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $rc = $objItem.GetOwner($strUser)

            Switch $rc
                Case 0
                    SetError(0)
                    Return $strUser
                Case 2
                    SetError(2)
                    Return "ACCESS DENIED"
                Case 3
                    SetError(3)
                    Return "INSUFFICIENT PRIVILEGE"
                Case 8
                    SetError(8)
                    Return "UNKNOWN FAILURE"
                Case 9
                    SetError(9)
                    Return "PATH NOT FOUND"
                Case 21
                    SetError(21)
                    Return "INVALID PARAMETER"
                Case Else
                    SetError(1)
                    Return -1
            EndSwitch
        Next
    EndIf
EndFunc   ;==>_ProcessGetOwner

But if the user closes notepad.exe when we are in the function, the line "$rc = $objItem.GetOwner($strUser)" returns an error and my script stops. How can i do to go out of the function when it happens without exit from autoit because why script must continue to run ?

Edited by Melba23
Added code tags

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

Hi, i cant test the code in this pc

What is the output error?

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Hi guys,

First thanks for your help...

The error is :

The requested action with this object has failed.:

$rc = $objItem.GetOwner($strUser)

$rc = $objItem.GetOwner($strUser)^ ERROR

It's always happen when i close notepad.exe manually (with a click or file/quit). Try it : open and close notepad.exe a lot of times and you'll see.

Edited by akorx

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

I cannot, because as soon as it opens it closes.

That is the function of the code?

Same here, it just closes, opened notepad numerous times, it wont even allow me to click to close because it closes much faster than i ever could.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

ok, now try to open notepad.exe a lot of time (via cmd and arrows, it's more easy) : you should have the error. Or modify the "sleep(10)" to sleep(3000).

Edited by akorx

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

I was able to reproduce after a while, but cannot like this.

Func TestProcess()
    $AllProcesses = ProcessList('notepad.exe')
    For $i = 1 To $AllProcesses[0][0]
        $ownwer = _ProcessGetOwner($AllProcesses[$i][1], ".")
        If StringInStr($AllProcesses[$i][0], "notepad.exe") And StringInStr($ownwer, @UserName) Then ProcessClose($AllProcesses[$i][0])
    Next
EndFunc   ;==>TestProcess

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I think the problem arises when, between the time it takes to verify $colItems as object and loop through $objItem getting the user, the window has been closed and hence we can not get the user for a non existing item.

Only way I can see to handle this is using a com error handler <- help file will show you example.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Thx JohnOne for this script, i didn't know "com error handler", i thk it's very usefuul to debug !

I'm agree with you about the origin of the problem : i call object that has been destroyed...

I've just test your script and i get this :

ERROR NUMBER : 80020009

WinDescription :

Script Line: -1

What does it mean and what can i do ?

I think i must exit from the function "_ProcessGetOwner" or return nothing when i see this type of error, but how can i do that ?

like this ?

$rc = $objItem.GetOwner($strUser)

Switch $rc

Case 0

SetError(0)

Return $strUser

Case 2

SetError(2)

Return "ACCESS DENIED"

Case 3

SetError(3)

Return "INSUFFICIENT PRIVILEGE"

Case 8

SetError(8)

Return "UNKNOWN FAILURE"

Case 9

SetError(9)

Return "PATH NOT FOUND"

Case 21

SetError(21)

Return "INVALID PARAMETER"

Case 80020009

SetError(0)

Return ""

Edited by akorx

AkorxMail akorx@yahoo.fr

Link to comment
Share on other sites

You don't need to do much, or anything really, just stop the scripts from crashing.

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

While 1
    Sleep(3000)
    TestProcess()
WEnd


Func TestProcess()
    $AllProcesses = ProcessList()
    For $i = 1 To $AllProcesses[0][0]
        If StringInStr($AllProcesses[$i][0], "notepad.exe") And StringInStr(_ProcessGetOwner($AllProcesses[$i][1], "."), @UserName) Then ProcessClose($AllProcesses[$i][0])
    Next
EndFunc   ;==>TestProcess

;===============================================================================
;
; Function Name:    _ProcessGetOwner()
; Description:      Get the owner of an open process
; Parameter(s):     $vProcess - PID of a process.
; $strComputer - Name of target computer
; Requirement(s):   AutoIt Beta v3.2.+
;                   cimwin32.dll (included with Windows)
; Return Value(s):  On Success - Returns owners username of process
;                     0 - Successful, returns username
;
; On Failure:       Returns friendly errormessage and sets @Error to:
;                     2 - Access denied
;                     3 - Insufficient privilege
;                     8 - Unknown failure
;                     21 - Path not found

; Author(s):        Andreas Fälldin
;===============================================================================

Func _ProcessGetOwner($vProcess, $strComputer)

    Local $i_PID = ProcessExists($vProcess)
    If Not $i_PID Then
        SetError(1)
        Return -1
    EndIf

    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $colItems = ""
    Local $strUser = ""

    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE ProcessID = " & $i_PID, "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $rc = $objItem.GetOwner($strUser)

            Switch $rc
                Case 0
                    SetError(0)
                    Return $strUser
                Case 2
                    SetError(2)
                    Return "ACCESS DENIED"
                Case 3
                    SetError(3)
                    Return "INSUFFICIENT PRIVILEGE"
                Case 8
                    SetError(8)
                    Return "UNKNOWN FAILURE"
                Case 9
                    SetError(9)
                    Return "PATH NOT FOUND"
                Case 21
                    SetError(21)
                    Return "INVALID PARAMETER"
                Case Else
                    SetError(1)
                    Return -1
            EndSwitch
        Next
    EndIf
EndFunc   ;==>_ProcessGetOwner

Func _ErrFunc($oError)
    Return
    #cs
        ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
        "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
        "err.description is: " & @TAB & $oError.description & @CRLF & _
        "err.source is: " & @TAB & $oError.source & @CRLF & _
        "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
        "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
        "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
        "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
        "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
    #ce
EndFunc   ;==>_ErrFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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...