Com Object.ref, setting value Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
anbra
Posts: 5
Joined: 03 Oct 2016, 10:15

Com Object.ref, setting value

Post by anbra » 25 Oct 2021, 10:56

Good day everyone,

I'm stuck at setting value at object, can't google out some simple examples, may any of you help with this ? :?
So, i create com Variables, and receive reference objects. And then i need to change value of one of the objects. But cannot, where is the problem ?

Code: Select all

DataType := ComVar()
DataValue := ComVar()

Object.GetXData("", DataType.ref, DataValue.ref) 
MsgBox % DataValue[][2]		; gives me stringA
DataValue[][2] := "stringB"	; does not set this value stringB, how do i set value ? __Set ?
MsgBox % DataValue[][2] 		; returns same stringA


; Below, com function from forum
	ComVar(Type=0xC)
	{
		static base := { __Get: "ComVarGet", __Set: "ComVarSet", __Delete: "ComVarDel" }
	; Create an array of 1 VARIANT.  This method allows built-in code to take
	; care of all conversions between VARIANT and AutoHotkey internal types.
		arr := ComObjArray(Type, 1)
	; Lock the array and retrieve a pointer to the VARIANT.
		DllCall("oleaut32\SafeArrayAccessData", "ptr", ComObjValue(arr), "ptr*", arr_data)
	; Store the array and an object which can be used to pass the VARIANT ByRef.
		return { ref: ComObject(0x4000|Type, arr_data), _: arr, base: base }
	}
anbra
Posts: 5
Joined: 03 Oct 2016, 10:15

Re: Com Object.ref, setting value

Post by anbra » 26 Oct 2021, 01:57

Bump,

I know there are crazy geniuses who can help :)
Might this help, from Autocad Description of com object.

Code: Select all

Object.GetXData("", DataType.ref, DataValue.ref)
Spoiler
teadrinker
Posts: 4314
Joined: 29 Mar 2015, 09:41
Contact:

Re: Com Object.ref, setting value

Post by teadrinker » 26 Oct 2021, 06:54

Just copy the entire code from the docs:

Code: Select all

ComVar(Type := 0xC)
{
    static base := { __Get: Func("ComVarGet"), __Set: Func("ComVarSet")
        , __Delete: Func("ComVarDel") } ; For base, see Custom Objects.
    
    ; Create a new object based on base.
    cv := {base: base}
    
    ; Allocate memory for a VARIANT to hold our value. VARIANT is used even
    ; when Type != VT_VARIANT so that VariantClear can be used by __delete.
    cv.SetCapacity("buf", 24), ptr := cv.GetAddress("buf")
    NumPut(0, NumPut(0, ptr+0, "int64"), "int64")
    
    if (Type != 0xC) { ; Not VT_VARIANT.
        NumPut(Type, ptr+0, "ushort") ; Set the variant type for __delete.
        ptr += 8 ; Point to the actual value.
    }
    
    ; Create an object which can be used to pass the variable ByRef.
    cv.ref := ComObject(0x4000|Type, ptr)
    
    return cv
}

ComVarGet(cv, p*) { ; Called when script accesses an unknown field.
    if p.MaxIndex() = "" ; No name/parameters, i.e. cv[]
        return cv.ref[]
}

ComVarSet(cv, v, p*) { ; Called when script sets an unknown field.
    if p.MaxIndex() = "" ; No name/parameters, i.e. cv[]:=v
        return cv.ref[] := v
}

ComVarDel(cv) { ; Called when the object is being freed.
    ; Depending on type, this may be needed to free the value, if set.
    DllCall("oleaut32\VariantClear", "ptr", cv.GetAddress("buf"))
}
anbra
Posts: 5
Joined: 03 Oct 2016, 10:15

Re: Com Object.ref, setting value

Post by anbra » 26 Oct 2021, 08:00

Thanks for the answer teadrinker,

I've tried to fiddle with this function, but at no gains. When i use GetXData, autocad gives two objects, the data is readable, but i cannot change it, as described in 1st post.

Code: Select all

	ComVarSet(cv, v, p*) { ; Called when script sets an unknown field.
		if p.MaxIndex() = "" ; No name/parameters, i.e. cv[]:=v
			return cv._[0] := v
	}
what does the variables "cv, v, p*" stand for ? my guess...
cv - ComVariable, at this case the XDataValue is object array of strings
v - values, the strings inside,
p* - indexes ?
And i've tried this before but it does not change anything, it it might help :

Code: Select all

	MsgBox % XDataValue[][2]		; gives answer string1
	ComVarSet(XDataValue, "String2", 2)
	MsgBox % XDataValue[][2]		; gives same answer string1
teadrinker
Posts: 4314
Joined: 29 Mar 2015, 09:41
Contact:

Re: Com Object.ref, setting value

Post by teadrinker » 26 Oct 2021, 08:11

anbra wrote: XDataValue
Variant (array of variants); output-only
Perhaps this is the thing?
anbra
Posts: 5
Joined: 03 Oct 2016, 10:15

Re: Com Object.ref, setting value

Post by anbra » 26 Oct 2021, 08:33

Yes, objects(variables) are output only - this means, that autocad outputs them for ahk, i cannot send code though this function to autocad.
But i'm not trying(currently).
There is same function to set Data back in:
Spoiler

What im trying to do, is to get Xdata, change variable, set Xdata back in.
I can set Xdata if i create Com variant object. I'm just stuck at changing the values in "get" object, to set it back modified. Are the variables in "DataValue.ref" linked with autocad, and are read-only ? and not "local" to AHK variable which i can modify at my will ?... Should i use command as .Clone() ?(did not work...)

Also, seperate question due to syntax.

Code: Select all

Object.GetXData("", XDataType.ref, XDataValue.ref)
MsgBox % XDataValue[][i]
What does the empty "[ ]" stands for ? if i write index as 0, gives no variable in msgbox.
anbra
Posts: 5
Joined: 03 Oct 2016, 10:15

Re: Com Object.ref, setting value  Topic is solved

Post by anbra » 26 Oct 2021, 08:52

Okay found an answer, it seems. I can not set the same variable though setXdata i reviced though getXdata.
Which is strange.
As i could get Point object and set it into other object that would use "point"...
At this point i had to recreate same object in AHK with VarArrayCreate. Which included the needed and modified data, and then just set it.
teadrinker
Posts: 4314
Joined: 29 Mar 2015, 09:41
Contact:

Re: Com Object.ref, setting value

Post by teadrinker » 26 Oct 2021, 12:04

anbra wrote: What does the empty "[ ]" stands for ?
ComObjActive() —> ByRef wrote:If a wrapper object's VarType includes the VT_BYREF (0x4000) flag, empty brackets [] can be used to read or write the referenced value.
This is just a syntactic feature of AHK.
Post Reply

Return to “Ask for Help (v1)”