Sign in to follow this  
Followers 0
Clipper34

Object event error handling

2 posts in this topic

Hey guys, i've been having some trouble with Obejcts. My question is in this code here:

#include <GUIConstants.au3>

$oIE = ObjCreate("Shell.Explorer.2")

GUICreate ( "Embedded Web control Test", 640, 580,(@DesktopWidth-640)/2, (@DesktopHeight-580)/2, BitOr($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS, $WS_CLIPCHILDREN))
$GUIActiveX  = GUICtrlCreateObj ( $oIE,   10, 40 , 600 , 360 )
$GUI_Button_Back = GuiCtrlCreateButton ("Back",  10, 420, 100,  30)
$GUI_Button_Forward = GuiCtrlCreateButton ("Forward", 120, 420, 100,  30)
$GUI_Button_Home = GuiCtrlCreateButton ("Home", 230, 420, 100,  30)
$GUI_Button_Stop = GuiCtrlCreateButton ("Stop", 330, 420, 100,  30)

GUISetState ()     

$oIE.navigate("http://www.google.com")

While 1
    $msg = GUIGetMsg()
    
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $GUI_Button_Home
            $oIE.navigate("http://www.google.com.com")
        Case $msg = $GUI_Button_Back
            $oIE.GoBack
        Case $msg = $GUI_Button_Forward
            $oIE.GoForward
        Case $msg = $GUI_Button_Stop
            $oIE.Quit
    EndSelect
    
Wend

GUIDelete ()

Exit
how would you do error handling. Say if you press forwards in the GUI, and an Object event error occurs, i have tryed things like this:
$Error = ObjEvent("AutoIt.Error", "Error")

Func Error()
    If $Error = @error Then
    MsgBox(0, "", "An error has occured!")
EndIf
EndFunc

SetError($Error)
and still haven't found a way to prevent object errors. Any ideas or suggestions? Thanks.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi, just a direct copy and paste from autoit help file "Function Reference -> Obj/COM Reference -> ObjEvent" section into your example.

#include <GUIConstants.au3>

$oIE = ObjCreate("Shell.Explorer.2")
Global $g_eventerror = 0
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

GUICreate ( "Embedded Web control Test", 640, 580,(@DesktopWidth-640)/2, (@DesktopHeight-580)/2, BitOr($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS, $WS_CLIPCHILDREN))
$GUIActiveX  = GUICtrlCreateObj ( $oIE,      10, 40 , 600 , 360 )
$GUI_Button_Back = GuiCtrlCreateButton ("Back",     10, 420, 100,  30)
$GUI_Button_Forward = GuiCtrlCreateButton ("Forward", 120, 420, 100,  30)
$GUI_Button_Home = GuiCtrlCreateButton ("Home",    230, 420, 100,  30)
$GUI_Button_Stop = GuiCtrlCreateButton ("Stop",    330, 420, 100,  30)

GUISetState ()      

$oIE.navigate("http://www.google.com")

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $GUI_Button_Home
            $oIE.navigate("http://www.google.com.com")
        Case $msg = $GUI_Button_Back
            $oIE.GoBack
        Case $msg = $GUI_Button_Forward
            $oIE.GoForward
        Case $msg = $GUI_Button_Stop
            $oIE.Quit
    EndSelect
    If $g_eventerror <> 0 Then $g_eventerror = 0
Wend

GUIDelete ()

Exit

Func MyErrFunc()

  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"      & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & hex($oMyError.number,8)  & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )
            
    Local $err = $oMyError.number
    If $err = 0 Then $err = -1
    
    $g_eventerror = $err  ; to check for after this function returns
Endfunc

Read through the whole of the Obj/COM Reference and you'll soon enough get the gist of it. Also have a look in AutoIt\Include folder at IE.au3 and at Word.au3, both have extensive com error handling and with a bit of study you you can soon enough see how the error handling is implemented for a better understanding.

Cheers

Edited by smashly

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