Jump to content

Catching COM errors when assigning a value to chained Object


Go to solution Solved by mLipok,

Recommended Posts

Posted
5 minutes ago, mLipok said:

just changed the example a little:

Okay.

But what I was wondering is, $oFake[] and $oTestObject[] are considered Maps in AutoIt's eyes, and thus,

$oFake.FakeObject = 1

Is really creating Map key "FakeObject", and assigning 1 to it.

So when you are doing something like

$oFake.FakeObject.Check2

Is AutoIt still going to assume it is an Object, rather than we are just badly combining vastly different variable types?

LibreOffice UDF  

Scite4AutoIt Spell-Checker Using LibreOffice

WPS Office adapter — Use MS Word UDFs with WPS Office!

LibreOffice API Inspector tools

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

Posted (edited)
Global $oCOM_ErrorHandler = ObjEvent("AutoIt.Error", _ComErrorHandler)

Global $oShell = ObjCreate("shell.application") ; Get the Windows Shell Object

Global $oFake = $oShell

$oFake.FakeObject = 1

ConsoleWrite($oFake.FakeObject.Check2 & @CRLF) ; _ComErrorHandler() is fired as this was already fixed in https://www.autoitscript.com/trac/autoit/ticket/3167

$oFake.FakeObject.Check2 = 1 ; _ComErrorHandler() is not fired and AutoIt ends with "Variable must be of type "Object""

Func _ComErrorHandler(ByRef $oError)
        #forceref $oError
        ConsoleWrite("! We intercepted a COM Error" & @CRLF)
EndFunc   ;==>_ComErrorHandler

Something like this?

Edited by donnyh13

LibreOffice UDF  

Scite4AutoIt Spell-Checker Using LibreOffice

WPS Office adapter — Use MS Word UDFs with WPS Office!

LibreOffice API Inspector tools

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

Posted
2 minutes ago, donnyh13 said:

Something like this?

Global $oCOM_ErrorHandler = ObjEvent("AutoIt.Error", _ComErrorHandler)

Global $oShell = ObjCreate("shell.application") ; Get the Windows Shell Object

Global $oFake = $oShell

$oFake.FakeObject = 1

ConsoleWrite($oFake.FakeObject.Check2 & @CRLF) ; _ComErrorHandler() is fired as this was already fixed in https://www.autoitscript.com/trac/autoit/ticket/3167

$oFake.FakeObject.Check3 = 1 ; _ComErrorHandler() is not fired and AutoIt ends with "Variable must be of type "Object""

Func _ComErrorHandler(ByRef $oError, $iLine = @ScriptLineNumber)
        #forceref $oError
        ConsoleWrite("@@(" & $iLine & ") : We intercepted a COM Error " & @CRLF)
EndFunc   ;==>_ComErrorHandler
@@(7) : We intercepted a COM Error 
@@(9) : We intercepted a COM Error 

@@(11) : We intercepted a COM Error 
"C:\Share\New AutoIt v3 Script.au3" (11) : ==> Variable must be of type "Object".:
$oFake.FakeObject.Check3 = 1
$oFake.FakeObject^ ERROR

..this is a bit more clear :)

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

Posted

...not even a "fake" or copy,
 

Global $oCOM_ErrorHandler = ObjEvent("AutoIt.Error", _ComErrorHandler) ; line 1
Global $oShell = ObjCreate("shell.application") ; line 2
$oShell.FakeObject = 1 ; line 3
$oShell.FakeObject.Check3 = 1 ; line 4
ConsoleWrite("hello" & @CRLF)  ; line 5
Func _ComErrorHandler(ByRef $oError, $iLine = @ScriptLineNumber)
        #forceref $oError
        ConsoleWrite('"' & @ScriptFullPath & '" (' & $iLine & ") : We intercepted a COM Error " & @CRLF)
EndFunc   ;==>_ComErrorHandler
"C:\Share\New AutoIt v3 Script.au3" (3) : We intercepted a COM Error 
"C:\Share\New AutoIt v3 Script.au3" (4) : We intercepted a COM Error 
"C:\Share\New AutoIt v3 Script.au3" (4) : ==> Variable must be of type "Object".:
$oShell.FakeObject.Check3 = 1
$oShell.FakeObject^ ERROR

so, it does catch the error but goes bananas anyways ( pardon the lingo, but "bananas" is what comes to mind )

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

Posted

..just for 🤷‍♂️

Global $oCOM_ErrorHandler = ObjEvent("AutoIt.Error", _ComErrorHandler) ; line 1
Global $oShell = ObjCreate("shell.application") ; line 2
Execute('$oShell.FakeObject = 1') ; line 3
Execute('$oShell.FakeObject.Check3 = 1') ; line 4
ConsoleWrite("hello" & @CRLF)  ; line 5
Func _ComErrorHandler(ByRef $oError, $iLine = @ScriptLineNumber)
        #forceref $oError
        ConsoleWrite('"' & @ScriptFullPath & '" (' & $iLine & ") : We intercepted a COM Error " & @CRLF)
EndFunc   ;==>_ComErrorHandler
"C:\Share\New AutoIt v3 Script.au3" (3) : We intercepted a COM Error 
"C:\Share\New AutoIt v3 Script.au3" (4) : We intercepted a COM Error 
hello

 

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

Posted
11 minutes ago, argumentum said:

so, it does catch the error but goes

Thanks.

It technically catches the call to

$oShell.FakeObject.Check3 = 1
----------^ Triggers Error
$oShell.FakeObject.Check3 = 1
-------------------^ Bananas

 

1 minute ago, argumentum said:

.just for 🤷‍♂️

Interesting.

LibreOffice UDF  

Scite4AutoIt Spell-Checker Using LibreOffice

WPS Office adapter — Use MS Word UDFs with WPS Office!

LibreOffice API Inspector tools

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

Posted (edited)

Hmm maybe I'm wrong. Yes you're right, it does seem to catch it, but still tries completing the operation.

Global $oCOM_ErrorHandler = ObjEvent("AutoIt.Error", _ComErrorHandler) ; line 1
Global $oShell = ObjCreate("shell.application") ; line 2
$oShell.FakeObject = 1 ; line 3
$oShell.FakeObject.Check3() ; line 4
$oShell.FakeObject.Check3 = 1 ; line 5
ConsoleWrite("hello" & @CRLF)  ; line 6
Func _ComErrorHandler(ByRef $oError, $iLine = @ScriptLineNumber)
        #forceref $oError
        ConsoleWrite('"' & @ScriptFullPath & '" (' & $iLine & ") : We intercepted a COM Error " & @CRLF)
EndFunc   ;==>_ComErrorHandler

"C:\Users\Owner\Desktop\New AutoIt v3 Script.au3" (3) : We intercepted a COM Error 
"C:\Users\Owner\Desktop\New AutoIt v3 Script.au3" (4) : We intercepted a COM Error 
"C:\Users\Owner\Desktop\New AutoIt v3 Script.au3" (5) : We intercepted a COM Error 
"C:\Users\Owner\Desktop\New AutoIt v3 Script.au3" (5) : ==> Variable must be of type "Object".:
$oShell.FakeObject.Check3 = 1
$oShell.FakeObject^ ERROR

 

Edited by donnyh13

LibreOffice UDF  

Scite4AutoIt Spell-Checker Using LibreOffice

WPS Office adapter — Use MS Word UDFs with WPS Office!

LibreOffice API Inspector tools

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

Posted (edited)

more complex example:

#include <AutoItConstants.au3>

Global $oCOM_ErrorHandler = ObjEvent("AutoIt.Error", _ComErrorHandler)

Global $oMAP_object[]
Global $oTestObject1_Shell = ObjCreate("shell.application")
Global $oTestObject2_Map[]
Global $_WatchPoint = @ScriptLineNumber

#Region ; testing FIX https://www.autoitscript.com/trac/autoit/ticket/3167
$_WatchPoint = @ScriptLineNumber
;~ $oTestObject1_Shell.Explore(@ScriptDir) ; works

$_WatchPoint = @ScriptLineNumber
$oTestObject1_Shell.Windows().Item(666).bzzzzzzz ; _ComErrorHandler() IS FIRED and AutoIt PROPERLY continue processing next line - due COM ERROR HANDLER was used and FIXED in #3167

$_WatchPoint = @ScriptLineNumber
$oTestObject1_Shell.Windows().Item(666).bzzzzzzz = 1 ; _ComErrorHandler() IS FIRED and AutoIt PROPERLY continue processing next line - due COM ERROR HANDLER was used and FIXED in #3167
#EndRegion ; testing https://www.autoitscript.com/trac/autoit/ticket/3167

$_WatchPoint = @ScriptLineNumber
$oMAP_object.Shell = $oTestObject1_Shell

$_WatchPoint = @ScriptLineNumber
$oMAP_object.Shell.Explore(@ScriptDir) ; works

$_WatchPoint = @ScriptLineNumber
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.Shell", VarGetType($oMAP_object.Shell))

$_WatchPoint = @ScriptLineNumber
; _ComErrorHandler() IS FIRED and AutoIt PROPERLY continue processing next line - due COM ERROR HANDLER was used and FIXED in #3167
$oMAP_object.Shell.Fake1(@ScriptDir)
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.Shell.Fake1(@ScriptDir)", '')

$_WatchPoint = @ScriptLineNumber
; _ComErrorHandler() IS FIRED and AutoIt PROPERLY continue processing next line - due COM ERROR HANDLER was used and FIXED in #3167
$oMAP_object.Shell.Fake2 = 2
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.Shell.Fake2 = 2", '')

$_WatchPoint = @ScriptLineNumber
; _ComErrorHandler() IS FIRED and AutoIt PROPERLY continue processing next line - due COM ERROR HANDLER was used and FIXED in #3167
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.Shell.Fake3", $oMAP_object.Shell.Fake3)

$_WatchPoint = @ScriptLineNumber
$oMAP_object.TestObject2_Map = $oTestObject2_Map
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.TestObject2_Map", VarGetType($oMAP_object.TestObject2_Map))

$_WatchPoint = @ScriptLineNumber
$oMAP_object.TestObject2_Map.Check1 = 1 ; create MAP element called "Check1" with value = 1

$_WatchPoint = @ScriptLineNumber
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.TestObject2_Map.Check1", $oMAP_object.TestObject2_Map.Check1)

$_WatchPoint = @ScriptLineNumber
MsgBox(0, @ScriptLineNumber & " : $oTestObject2_Map.Check1", $oTestObject2_Map.Check1)

$_WatchPoint = @ScriptLineNumber
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.TestObject2_Map.Check1", $oMAP_object.TestObject2_Map.Check1)

$_WatchPoint = @ScriptLineNumber
$oMAP_object.FakeObject1 = 1 ; create MAP element called "FakeObject" with value = 1

$_WatchPoint = @ScriptLineNumber
; in the following line _ComErrorHandler() IS NOT FIRED although it has already been fixed in https://www.autoitscript.com/trac/autoit/ticket/3167 = IMHO this is AutoIt issue
;  ... but maybe it is intentional to not FIRE _ComErrorHandler() as $oMAP_object.FakeObject1 is treated as MAP ELEMENT thus Check2 is also treated as MAP element and thus can be created on the fly
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.FakeObject1.Check2", "Result=" & $oMAP_object.FakeObject1.Check2)

$_WatchPoint = @ScriptLineNumber
; _ComErrorHandler() IS FIRED and AutoIt PROPERLY continue processing next line - due COM ERROR HANDLER was used and FIXED in #3167
MsgBox(0, @ScriptLineNumber & " : $oCOM_ErrorHandler.FakeObject1.Check3", $oCOM_ErrorHandler.FakeObject1.Check3)

$_WatchPoint = @ScriptLineNumber
; in the following line _ComErrorHandler() IS FIRED but AutoIt ends with (Variable must be of type "Object") = IMHO this is AutoIt issue
;  ... this is the case @donnyh13 reported at the begining = IMHO this is AutoIt issue
$oCOM_ErrorHandler.FakeObject2.Check4 = 1
MsgBox(0, @ScriptLineNumber & " : $oCOM_ErrorHandler.FakeObject2.Check4", "")

$_WatchPoint = @ScriptLineNumber
; _ComErrorHandler() IS FIRED and AutoIt PROPERLY continue processing next line - due COM ERROR HANDLER was used and FIXED in #3167
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.FakeObject3.Check5", $oMAP_object.FakeObject3.Check5)

$_WatchPoint = @ScriptLineNumber
; in the following line _ComErrorHandler() IS NOT FIRED and AutoIt ends with (Variable must be of type "Object") = IMHO this is AutoIt issue
;  ... this is the case @donnyh13 reported at the begining = IMHO this is AutoIt issue
$oMAP_object.FakeMapElement.Check6 = 1
MsgBox(0, @ScriptLineNumber & " : $oMAP_object.FakeMapElement.Check6", "")

Func _ComErrorHandler(ByRef $oError)
    #forceref $oError
    ConsoleWrite("! We intercepted a COM Error : Last @ScriptLineNumber (WatchPoint) =" & $_WatchPoint & @CRLF)
EndFunc   ;==>_ComErrorHandler

#REMARK =>  .Check2 needs To be explained
#REMARK =>  both .Check4 and .Check6 should fires _ComErrorHandler() and show the console message also AutoIt should not ends With: Variable must be of type "Object"

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Posted
Global $oCOM_ErrorHandler = ObjEvent("AutoIt.Error", _ComErrorHandler) ; line 1
Global $oShell = ObjCreate("shell.application") ; line 2
;~ If IsObj($oObject) Then would be advisable everywhere
$oShell.FakeObject = 1 ; line 4
Execute('$oShell.FakeObject.Check3 = 1') ; line 5 ; this is "saved" from crashing
;~ $oShell.FakeObject.Check3 = 1 ; line 6 ; this is will "oops"
$oFakeObject = $oShell.FakeObject ; line 7 ; flatten it first..
$oFakeObject.Check3 = 1 ; line 8 ; ..then call it, to overcome the current AutoIt3 behaviour
ConsoleWrite("hello" & @CRLF)  ; line 9
Func _ComErrorHandler(ByRef $oError, $iLine = @ScriptLineNumber)
        #forceref $oError
        ConsoleWrite('"' & @ScriptFullPath & '" (' & $iLine & ") : We intercepted a COM Error " & @CRLF)
EndFunc   ;==>_ComErrorHandler

..had to do stuff like this working with $oIE :)

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

Posted

Unfortunately that looks like it will have to be my go-to method as well 😕 , the neat-freak in me is crying a little. :'(

Thanks for the input everyone! :D

LibreOffice UDF  

Scite4AutoIt Spell-Checker Using LibreOffice

WPS Office adapter — Use MS Word UDFs with WPS Office!

LibreOffice API Inspector tools

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

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
×
×
  • Create New...