Jump to content

Dynamic properties with AutoItObject


ivan
 Share

Recommended Posts

Ok, I'm stuck here. I'm using AutoItObject on a script which needs to assign and retreive properties dynamically. Adding properties is can be handled with _AutoItObject_AddProperty and reading its value can be handled with Execute. However, assignment is a totally different thing. I've been testing with the script below, it requires obviously AutoItObject_v1.0.0.

Edited: added a method ReadyProperty and SetProperty. What I'm after is the SetProperty. Actually, if someone could also point in the right direction for testing for the existence of a member, like MemberExists method, I'd be over the moon.

Any help would be much appreciated.

Thx.

#include "AutoItObject_v1.0.0/AutoItObject.au3"
_AutoItObject_StartUp()
Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
Global $Prop = "Prop1"
Global $oDynamic = oDynamic_Constructor($Prop, 'Label1')

; check Assignment
$oDynamic.SetProperty($Prop, 'new value')
MsgBox(0, 'Check', $oDynamic.Prop1)


; read
MsgBox(0, 'Read', $oDynamic.ReadyProperty($Prop))

Func oDynamic_Constructor($pPropertyName, $pValue)
    Local $oObj = _AutoItObject_Create()
    _AutoItObject_AddProperty($oObj, $pPropertyName, $ELSCOPE_PUBLIC, $pValue)
    _AutoItObject_AddMethod($oObj, "ReadyProperty", "ReadyProperty")
    _AutoItObject_AddMethod($oObj, "SetProperty", "SetProperty")
    Return $oObj

EndFunc   ;==>oDynamic_Constructor

Func SetProperty($oSelf, $pPropName, $pValue)
    Execute("$oSelf." & $pPropName & '=' & $pValue)
    If Execute("$oSelf." & $pPropName) = $pValue Then MsgBox(0, 'Assign succeeded', $oSelf.ReadyProperty($pPropName))
    Assign("oSelf." & $pPropName, $pValue, 4)
    If Execute("$oSelf." & $pPropName) = $pValue Then MsgBox(0, 'Assign succeeded', $oSelf.ReadyProperty($pPropName))
    Eval("oSelf." & $pPropName & '=' & $pValue)
    If Execute("$oSelf." & $pPropName) = $pValue Then MsgBox(0, 'Assign succeeded', $oSelf.ReadyProperty($pPropName))

EndFunc   ;==>SetProperty

Func ReadyProperty($oSelf, $pPropName)
    Return Execute("$oSelf." & $pPropName)
EndFunc   ;==>ReadyProperty

Func _ErrFunc()
    ConsoleWrite("! COM Error !  Number: 0x" & Hex($oError.number, 8) & "   ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
    Return
EndFunc   ;==>_ErrFunc
Edited by ivan
Link to comment
Share on other sites

Why don't you go from the start and add another method Assign. Something like:

Func oDynamic_Constructor($pPropertyName, $pValue)
    Local $oObj = _AutoItObject_Create()
    _AutoItObject_AddProperty($oObj, $pPropertyName, $ELSCOPE_PUBLIC, $pValue)
    ;...
    _AutoItObject_AddMethod($oObj, "Assign", "_AutoItObject_AddProperty")
    Return $oObj
EndFunc

Then you could do:

Global $Prop = "Prop1"
Global $oDynamic = oDynamic_Constructor($Prop, 'Label1')

ConsoleWrite($oDynamic.Prop1 & @CRLF)
; Assign
$oDynamic.Assign("Prop1", $ELSCOPE_PUBLIC, "new value")

ConsoleWrite($oDynamic.Prop1 & @CRLF)

Is that what you want?

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Why don't you go from the start and add another method Assign. Something like:

Func oDynamic_Constructor($pPropertyName, $pValue)
    Local $oObj = _AutoItObject_Create()
    _AutoItObject_AddProperty($oObj, $pPropertyName, $ELSCOPE_PUBLIC, $pValue)
    ;...
    _AutoItObject_AddMethod($oObj, "Assign", "_AutoItObject_AddProperty")
    Return $oObj
EndFunc

Then you could do:

Global $Prop = "Prop1"
Global $oDynamic = oDynamic_Constructor($Prop, 'Label1')

ConsoleWrite($oDynamic.Prop1 & @CRLF)
; Assign
$oDynamic.Assign("Prop1", $ELSCOPE_PUBLIC, "new value")

ConsoleWrite($oDynamic.Prop1 & @CRLF)

Is that what you want?

@Trancexx:

Thanks so much!!!!

Forgive my stupidity, I've been trying to get this to work for hours, and your solution works like a charm. Out of curiosity, the use of _AutoItObject_AddProperty would create a new property with the same name or overwrite the existing one? Just in case you wish to help me further, how can I test for the existence of a member?

IVAN

Link to comment
Share on other sites

Just in case you wish to help me further, how can I test for the existence of a member?

This is not implemented. You could try to access it, a COM-error will occur if the member is not present.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Use it then:

ConsoleWrite("NonExistingProperty = " & $oDynamic.NonExistingProperty & @CRLF)
If $oError.number = 0x80020006 Then
    $oDynamic.Assign("NonExistingProperty", $ELSCOPE_PUBLIC, "Exists")
EndIf
ConsoleWrite("NonExistingProperty = " & $oDynamic.NonExistingProperty & @CRLF)

This is so cool!!! I can forget about continuing my member testing... I thought that in compiled exes the com object errors would always end in a crash.

Thanks. I had already created a property array containing the names of all properties PropertyList, a method to add new properties (NewProperty) logging each new entry and finally a method PropertyExists. I was about to start doing something similar for methods. Like in the code below:

#include <AutoItObject_v1.0.0/AutoItObject.au3>

_AutoItObject_StartUp()
Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
Global $Prop = "Prop1"
Global $oDynamic = oDynamic_Constructor()
MsgBox(0, 'start', $oDynamic.PropertyList(0))
$oDynamic.NewProperty($Prop, 'Label1')
MsgBox(0, 'added', $oDynamic.PropertyList(0))

MsgBox(0, 'Check', $oDynamic.Prop1)

; check Assignment
$oDynamic.Assign($Prop, $ELSCOPE_PUBLIC, "new value")

MsgBox(0, 'Check', $oDynamic.Prop1)

If $oDynamic.NewProperty($Prop, 'Label1') Then MsgBox(0, 'Check error', 'error should not add')
If $oDynamic.NewProperty($Prop, 'Label1') Then MsgBox(0, 'Check error', 'error should not add')
; read
MsgBox(0, 'Read', $oDynamic.ReadyProperty($Prop))

MsgBox(0, 'count', $oDynamic.PropertyList(0))
; release
$oDynamic = 0

Func oDynamic_Constructor()
    Local $oObj = _AutoItObject_Create()
    Dim $aPropertyList[1] = [1]
    _AutoItObject_AddProperty($oObj, "PropertyList", $ELSCOPE_PUBLIC, $aPropertyList)
    _AutoItObject_AddMethod($oObj, "NewProperty", "NewProperty")
    _AutoItObject_AddMethod($oObj, "ReadyProperty", "ReadyProperty")
    _AutoItObject_AddMethod($oObj, "Assign", "_AutoItObject_AddProperty")
    _AutoItObject_AddMethod($oObj, "PropertyExists", "PropertyExists")

    Return $oObj

EndFunc   ;==>oDynamic_Constructor

; adds a new property if it does not already exist
Func NewProperty($oSelf, $pPropertyName, $pValue)
    Dim $aPropertyList = $oSelf.PropertyList
    If $oSelf.PropertyExists($pPropertyName) Then Return SetError(1, 0, 0)
    ReDim $aPropertyList[UBound($aPropertyList) + 1]
    $aPropertyList[UBound($aPropertyList) - 1] = $pPropertyName
    $aPropertyList[0] = UBound($aPropertyList)
    $oSelf.PropertyList = $aPropertyList
    _AutoItObject_AddProperty($oSelf, $pPropertyName, $ELSCOPE_PUBLIC, $pValue)
    Return SetError(0, 0, 1)
EndFunc   ;==>NewProperty

Func PropertyExists($oSelf, $pPropName)
    Dim $aPropertyList = $oSelf.PropertyList
    If $aPropertyList[0] <= 1 Then Return False
    For $iProp = 1 To UBound($aPropertyList) - 1 Step 1
        If $pPropName = $aPropertyList[$iProp] Then Return True
    Next
    Return False
EndFunc   ;==>PropertyExists

Func ReadyProperty($oSelf, $pPropName)
    If $oSelf.PropertyExists($pPropName) Then Return SetError(0, 0, Execute("$oSelf." & $pPropName))
    Return SetError(1, 0, 0)
EndFunc   ;==>ReadyProperty

Func _ErrFunc()
    ConsoleWrite("! COM Error !  Number: 0x" & Hex($oError.number, 8) & "   ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
    Return
EndFunc   ;==>_ErrFunc
Edited by ivan
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...