MightyR1
(MM club member)
2002-07-10 11:51 AM
error when checking if object was created

Hi all again,

I just upgraded to KIX4.10a and I've encountered a problem.

I use KIXGui to create a form (and later a progressbar):
code:
$root = CreateObject("KIXGUI.Desktop")
;
If Len($root) = 0
$x = MessageBox("Error creating KIXGUI
Object." + $nl + $nl + "Please register
KIXGUI.dll.", "tslogin.kix", 16+4096)
Exit (1)
EndIf

But when I run it I get:

Script error: IDispatch pointers not allowed in expressions!
If Len($root) = 0


How do I check if the object is created?
Where is this function change documented?


LonkeroAdministrator
(KiX Master Guru)
2002-07-10 11:54 AM
Re: error when checking if object was created

yes.
object-handles are not kixtart variables but pointers.

you can't check for the length.

rather I suggest you checking error and/or vartype.

cheers,


LonkeroAdministrator
(KiX Master Guru)
2002-07-10 11:55 AM
Re: error when checking if object was created

oh and best documentation is here.

just read:
http://81.17.37.55/board/ultimatebb.php?ubb=get_topic;f=13;t=000194

cheers,


MightyR1
(MM club member)
2002-07-11 12:26 AM
Re: error when checking if object was created

[Confused] Strange.... [Confused]

It worked with Kix4.02!!!

This comes from an KIXGUI example:
quote:

Break on
;
; Inputbox.k2k
;
; A demonstration script for KiXGUI
;
; Kixtart v4.01
; KiXGUI 1.0
;
; Done by Alex.H
;

$root = CreateObject("KiXGUI.Desktop")
If Len($root)=0 ; KiXGUI not registered or not present
? "KiXGUI not registered"
Exit 1
EndIf

So there must be a change somewhere [Wink]


MightyR1
(MM club member)
2002-07-11 12:41 AM
Re: error when checking if object was created

Thnx Lonkero,

this works:
code:
if not $root
? "KiXGUI not registered."
endif



LonkeroAdministrator
(KiX Master Guru)
2002-07-11 12:44 AM
Re: error when checking if object was created

yes there is change.
but the change is not wrong.
as I said it's objecthandle-pointer.

there is no way kix can do pointer to string translation (as pointer is not kix variabletype).

it was perhaps handled before something like:
if it can't be translated there is something.
and that's why len returned something.

for some reason (maybe some new feature or just disallowing wrong syntax) ruud has removed it.

so, it's more than expected error.
just it's not so well documented.
haven't tried but what happens with 4.02 if you do:
CreateObject("KIXGUI.Desktop")

without placing the handle to variable?

it's just the same thing.


ShawnAdministrator
(KiX Supporter)
2002-07-10 02:05 PM
Re: error when checking if object was created

Even this syntax:

if not $root

isn't best pratice anymore (not so much with KixGUI, but with most other MS COM objects] as Ruud introduced DEFAULT PROPERTIES with 4.10 - which has changed the KOM landscape quite a bit.

Suggest best practice is to do this:

$root = CreateObject("KiXGUI.Desktop")
if @ERROR ...

or maybe this (not a big fan) ...

$root = CreateObject("KiXGUI.Desktop")
if vartype($root) = 9 ; Object Handle

[shrug]


LonkeroAdministrator
(KiX Master Guru)
2002-07-10 02:10 PM
Re: error when checking if object was created

shawn, just said the same in first post.
well, he will return when that "if not" will stop working [Roll Eyes]
and then we can point back to this topic...


ShawnAdministrator
(KiX Supporter)
2002-07-10 02:12 PM
Re: error when checking if object was created

yap - as usual, your way ahead of me.

LonkeroAdministrator
(KiX Master Guru)
2002-07-10 02:14 PM
Re: error when checking if object was created

well, it probably is just that it's morning there and that makes you... well your fingers little slow.

Chris S.
(MM club member)
2002-07-10 03:11 PM
Re: error when checking if object was created

I chose the "if not $root" checking because 1) it works (at least for now) and 2) because I use KiXGUI with my logon script and I wrote it to handle both KiXGUI output and console output if the client is unable to register KiXGUI for some reason. Therefore, I have to make checks throughout my script to verify that KiXGUI is running. My original script was based on the example in the KiXGUI.zip file which I'm finding out now is rife with things not considered 'best practice.'

I may change it in the future to something more like this...

code:
$root=createobject("KiXGUI.Desktop")
if @error
? "KiXGUI not registered."
else
$GUIOK=1
endif



LonkeroAdministrator
(KiX Master Guru)
2002-07-10 03:14 PM
Re: error when checking if object was created

alex could help with this.
when he has last time updated the package?
I have somekind of memory-block that the development has stopped or uttleast no new versions has been introduced here.

so, probably the documentation is not 4.10 compliant...

alex?

[ 10 July 2002, 15:15: Message edited by: Lonkero ]


Alex.H
(Seasoned Scripter)
2002-07-12 01:48 AM
Re: error when checking if object was created

Last kixgui release was little after kixtart 4.02, so yes, no compliance with 4.10a

MightyR1
(MM club member)
2002-07-12 11:23 PM
Re: error when checking if object was created

Guys,

the if not $root is functioning fine.
But you suggest I'd use if @error instead!

Why? I don't understand the "not so much with KixGUI, but with most other MS COM objects" thingy.

Can you make some things more clear for me?


Sealeopard
(KiX Master)
2002-07-13 12:06 AM
Re: error when checking if object was created

Because it's not really a variable but a pointer. By using @ERROR you make sure that there was no error in creating the object. This would be clean and proper programming syntax.
code:
$object=CreateObject('KIXGUI.Desktop')
if @ERROR
? 'Unable to create object 'KIXGUI.Desktop'
else
; now you can do whatever you want to do with your object
endif
; and don't forget to clean up your object after use
$object = ''



Howard Bullock
(KiX Supporter)
2002-07-13 07:56 AM
Re: error when checking if object was created

What if we wrap it in a UDF and bury the object check in there?

Since KiXtart 4.10 supports default properties and methods, how does one distinguish between an object with default properties or methods and one without any default properites?

The following code serves two purposes. First, it is an example of the UDF discussed above. Second, attempting to print the object yields text for the "Excel.Application" object, a zero (Boolean) for a failure to create the object, and a catastrophic script failure for the "WScript.Shell" object which apparently doesn't have default properties.

I used two if statement just to verify the 'NOT' or lack thereof did not behave differently than expected. Execute with 4.10.
code:
Break on
? "Object1"
$object1=udfCreateObject('Excel.Application')
if $object1
? "Object Exists"
? $Object1
endif
if not $object1
? "No Object"
? $Object1
endif

? ? "Object2"
$object2=udfCreateObject('Bogus')
if $object2
? "Object Exists"
? $Object2
endif
if not $object2
? "No Object"
? $Object2
endif

? ? "Object3"
$object3=udfCreateObject('WScript.Shell')
if $object3
? "Object Exists"
? $Object3
endif
if not $object3
? "No Object"
? $Object3
endif


Function udfCreateObject($A)
Dim $B
$B = CreateObject($A)
If VarTypeName($B) = "Object"
$udfCreateObject = $B
Else
? "Error creating object '"'$A'"'"
$udfCreateObject = NOT 1
Endif
Endfunction



[ 13 July 2002, 08:01: Message edited by: Howard Bullock ]