Sign in to follow this  
Followers 0
Inververs

way to know IsObjExists

17 posts in this topic

is there any way to know that the object exists?

something like this:

$oIE=ObjCreate('InternetExplorer.Application')
$oIE.quit() ;close ie

If IsObj($oIE) Then
 ConsoleWrite("Var Is Obj but no obj exist" & @LF)
EndIf

If _ObjExist($oIE) Then
$oIE.some_method_here
EndIf

Share this post


Link to post
Share on other sites



you can get the Property=HWND, and see it it returns a valid handle:

#include <IE.au3>
$oIE=ObjCreate('InternetExplorer.Application')
$oIE.quit()
$hwnd = _IEPropertyGet ( $oIE, "hwnd")
If IsHWnd ($hwnd) Then
 MsgBox ( 1,1,1)
Else
 MsgBox ( 1,1,0)
EndIf

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

The easiest way to check to make sure that the object exists is check it after you create/connect to it. In this example, if you close IE, you should clear your object variable, I'm not sure if there's a way to check to see if the object still exists, but you shouldn't be using such poor programming techniques where you're closing objects like that and then trying to work with them later.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Don't check for existance check for errors:

$oIE=ObjCreate('InternetExplorer.Application')
If @error then MsgBox(16, "Error", "Error creating the IE object: " & @error)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

$oIE.quit() ;close ie -

here only as an example.

IE object may cease to exist when you close it, kill etc

Share this post


Link to post
Share on other sites

$oIE=ObjCreate('InternetExplorer.Application')
$oIE.quit() ;close ie
$oIE.hwnd() ;it will crash here, no $oIE exists :bye:

Share this post


Link to post
Share on other sites

If your are closing $oIE and it is no longer of use then wouldnt the below work

$oIE.quit()

$oIE = 0

VarGetType($oIE) will equal integer

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Question: A bit off topic but related...

I have a web page that is slow to load at times, and I work in frames. So I have to check after an attach statement to see if it really attached. I've been doing it by checking isobj()

$Count = 0
Do
     $Count+=1
     $oIE = _IEAttachHDL($DQMwHndl)
     Sleep(200)
     If $Count > 10 Then
           MsgBox(0,$DQMerror1,$DQMerror2)
           Return
     EndIf
Until IsObj($oIE)
This works, but takes up CPU time checking a lot until the page loads. ($DQMwHndl is the handle to the IECREATE(web address) )After the attach, I have to check for the frame, and form, using the same logic, or sometimes my script fails, as it cant get past all that stuff.The short version:
$oIE = _IEAttachHDL($DQMwHndl)$oIFrame = _IEFrameGetObjByName($oIE, "ifmPage")$oForm = _IEFormGetObjByName($oIFrame, "frmADT")$oSubmit = _IEGetObjByName($oForm, "button2")$oSearchCompID = _IEFormElementGetObjByName($oForm, $CompanyPicker)_IEFormElementSetValue($oSearchCompID,$ClientFacility)_IEAction($oSubmit, "click")the long version is what works, with each line above having a loop to check IsObj().  Basically because _loadwait() really doesn't work well.  So each time I click a link, i have to verify all the objects again.

Is there a better way to make sure each object is a valid object other than what I preset here (too lengthy-not a clean script, too much CPU time)

Thanks!

Edited by kalel69

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

People, are you kidding me. object IE I cited as an example only.

Well, we formulate the question differently.

1) create some obj: $oOBJ = ObjCreate("some.object")

2) what has happened and the object has ceased to exist.

3) but my application does not know about it

4) and after the method call closes with an error ($oOBJ.some_method).

Leads to the question: how to check the existence of the object??

Edited by Odin

Share this post


Link to post
Share on other sites

As I've already mentioned in post #4. Check @error after the method tried to access an non existing object.

Which version of AutoIt do you use?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

water you don't understand what Odin is saying.

Odin that's your responsibility, one way or the other. Usually if the object provides method to destruct itself then it also provides a way to notify you when that happens.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Simulate some situation:

$oIE=ObjCreate('InternetExplorer.Application')
If @error Then Exit 99
;OK no error here
$oIE.navigate("about:blank")
$oIE.visible = True
;Ok now it is visible - close it manual;
;~ Simulating the destruction of the object/ just close the window
MsgBox(64,"Close IE","Please, close IE window by click on [x] button. Press ok to continue." & @CRLF)
;something happened and the object has ceased to exist.
;but the program does not know what the object is destroyed
;try to call some method
$oIE.navigate("google.com")
;yes, last beta 3.3.9.4 we can check @error macro
If @error Then
ConsoleWrite('ooops, some error here' & @LF)
;but what's the reason for this error??
;navigate method definitely the right ..
EndIf

so you need a way to know that an object still exists.

Thank you for understanding. :(

Share this post


Link to post
Share on other sites

The value of @error (translated to hex) and a COM error handler can give you more information.

Check ObjEvent how to set up an error handler.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

yes, I know that there are "COM error handler"

but I need not monitor the com error, but only the existence of the object.

$oIE_1 = ObjCreate('InternetExplorer.Application')
$oIE_2 = ObjCreate('InternetExplorer.Application')
$oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
AdlibRegister("_OBjExterminator",5000) ;))

While Sleep(100)
;do something in loop
;~ If an error occurs, which of these objects cease to exist?
$oIE_1.readystate() ;maybe this?
$oIE_2.readystate() ;or maybe this?
;or maybe this?
WEnd

Func _ErrFunc($oError)
; com error trap
; a useful thing
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)
EndFunc ;==>_ErrFunc

Func _OBjExterminator()
;lol
ConsoleWrite("! no one obj" & @LF)
ProcessClose('iexplore.exe')
EndFunc

Share this post


Link to post
Share on other sites

The object (created by ObjCreate) is still there, the Application (IE or whatever) is gone!

If the Application you start using ObjCreate no longer exists all references to this application (in your example $oIE_1 and $oIE_2) are no longer valid. If you try to access a property or execute a method you will receive an error!

First thing to do is to make sure the user can't end the application (do not make it visible until your script has finished processing).

Second thing to do is to check for @error after you access the object (properties or methods) and then act accordingly.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

:facepalm:

Second thing to do is to check for @error after you access the object (properties or methods) and then act accordingly

OK, lets use a "com error handler"

;look at this code

$oOBj.someMethod()
If @error then ...
$oOBj.someMethod2()
If @error then ...
$oOBj.someMethod3()
If @error then ...
$oOBj.someMethod()
If @error then ...
;several million checks
;


Func _CommError()
return SetError(1)
EndFunc

Example 2. without error handler

If Not _ISOBJEEXIST($oOBJ) Then
;if the object does not exist, but there is no sense to perform this code
$oOBj.someMethod1()
$oOBj.someMethod2()
$oOBj.someMethod3()
EndIF

First thing to do is to make sure the user can't end the application (do not make it visible until your script has finished processing).

but I have quite the opposite :) And the user should be able to close the ie application.. Edited by Odin

Share this post


Link to post
Share on other sites

:facepalm:

Ok, I give up. Good luck!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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