Jump to content

Recommended Posts

Posted (edited)

Just as topic says - Is there any chance to assign variable to object property with string representing an expression? 

I tried:

Execute('$oMyObject.Name = "Some name"')

also

Assign("$oMyObject.Name", "Some name", 4)

but does not work

Any idea?

Edit: I just found similar thread but from 2012 and it looks like it's not possible with AutoIt that time. Can someone confirm if anything changed?

 

Edited by maniootek
Posted

Didn't find a way to do it with the property name in a variable. Doesn't seem to be possible.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted

I didn't implement this solution in my code because I do not use code I do/can not fully understand. I wouldn't be able to debug such code if the need arises in the near or far future.
So I took the long and winding road ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Posted (edited)

A simpler way, if it's okay for you to set a property value by passing "strings" to a function instead of using a single string in an Execute () statement, then you can use a "magic" way provided by a magical girl, @trancexx, published at this link:

   here I simply wrapped his magic potion within this function:

Func _SetProperty($oObj, $sProperty, $vData)
    ; by Trancexx
    ; https://www.autoitscript.com/forum/topic/200129-set-object-properties-with-propertyname-and-value-taken-from-an-array/?do=findComment&comment=1436379

    ; Some constants used in code
    Const $tagIUnknown = "QueryInterface hresult(ptr;ptr*);" & _
            "AddRef dword();" & _
            "Release dword();"

    Const $tagIDispatch = $tagIUnknown & _
            "GetTypeInfoCount hresult(dword*);" & _
            "GetTypeInfo hresult(dword;dword;ptr*);" & _
            "GetIDsOfNames hresult(struct*;struct*;dword;dword;struct*);" & _
            "Invoke hresult(uint;struct*;dword;word;struct*;struct*;ptr;uint*);"

    Const $DISPID_PROPERTYPUT = -3
    Const $DISPATCH_PROPERTYPUT = 4
    Const $LOCALE_SYSTEM_DEFAULT = 0x800
    Const $tIID_NULL = DllStructCreate("byte[16]")
    Const $tagDISPPARAMS = "ptr rgvarg;ptr rgdispidNamedArgs;uint cArgs;uint cNamedArgs;"
    Const $VT_I4 = 3
    Const $tVARIANT = "word vt;word r1;word r2;word r3;ptr data; ptr"
    Const $sIID_IDispatch = "{00020400-0000-0000-C000-000000000046}"

    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; Superposed object on top of the original one
    Local $oObjMy = ObjCreateInterface($oObj, $sIID_IDispatch, $tagIDispatch, False)

    ; Collect ID number of the function/property/method, whatever
    $tDisp = DllStructCreate("dword")
    $tName = DllStructCreate("ptr")
    $tN = DllStructCreate("wchar[" & StringLen($sProperty) + 1 & "]")
    DllStructSetData($tN, 1, $sProperty)
    DllStructSetData($tName, 1, DllStructGetPtr($tN))

    $oObjMy.GetIDsOfNames($tIID_NULL, $tName, 1, $LOCALE_SYSTEM_DEFAULT, $tDisp)
    ; Tadaaa!
    $iDispId = DllStructGetData($tDisp, 1)


    ; Now build disp parameters
    $tDISPPARAMS = DllStructCreate($tagDISPPARAMS)
    $tDISPPARAMS.cNamedArgs = 1
    $tDispidNamed = DllStructCreate("uint")
    DllStructSetData($tDispidNamed, 1, $DISPID_PROPERTYPUT)
    $tDISPPARAMS.rgdispidNamedArgs = DllStructGetPtr($tDispidNamed)
    $tDISPPARAMS.cArgs = 1
    $tVar = DllStructCreate($tVARIANT)
    $tDISPPARAMS.rgvarg = DllStructGetPtr($tVar)

    ; Set desired value
    $tVar.vt = $VT_I4
    $tVar.data = $vData

    ; And call it
    $iRet = $oObjMy.Invoke($iDispId, $tIID_NULL, 0x800, $DISPATCH_PROPERTYPUT, $tDISPPARAMS, 0, 0, 0)

    Return (Hex($iRet, 8))
    ; ConsoleWrite(">>> Returned hresult = " & Hex($iRet, 8) & @CRLF)
    ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
EndFunc   ;==>_SetProperty

here an example of use: https://www.autoitscript.com/forum/topic/200129-set-object-properties-with-propertyname-and-value-taken-from-an-array/?do=findComment&comment=1436386

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted

This really is a gem, I mean for what it does, as for how it does it, unfortunately for me, I can't understand even one percent of that stuff, but I'm fascinated by it anyway,
after all, only a wizard like @trancexx, or rather, only the queen of developers can produce these special effects :thumbsup:
Thank you so much for this other pearl of yours

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted
On 5/3/2020 at 8:32 AM, Chimp said:

This really is a gem, I mean for what it does, as for how it does it, unfortunately for me, I can't understand even one percent of that stuff, but I'm fascinated by it anyway,
after all, only a wizard like @trancexx, or rather, only the queen of developers can produce these special effects :thumbsup:
Thank you so much for this other pearl of yours

Actually it's pretty straightforward. An object is created that has a pointer to the original object as its property, and all the calls to methods of this new object are simply translated to the original one. Obviously GetIDsOfNames and Invoke of the new object checks for call to the new methods and alters the parameters array accordingly before calling the original.

The only thing that was truly a challenge was how to free dynamically allocated memory from within itself. Doing it from x64 code is easy due to specific calling convention and execution algorithm, but from x86 it's nightmare until you figure out that it is not the spoon that bends, lol. There is no spoon.

The code is super interesting, I wish I had a chance to see something like that ten years ago, and be able to learn from it. I would have asked thousands of questions. Who knows... maybe I'd be ruling the world now, and not that fuc*ker. This way I had to do it all by myself with little chance of ruling. The. World.

♡♡♡

.

eMyvnE

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...