sancarn / DynaCLR.JS

An dynamic and portable CLRCompiler which allows you to compile and run C#/VB .NET code dynamically on Windows OS. - A portable reincarnation of Edge.JS
8 stars 1 forks source link

Returning Collections from DynaCLR #2

Open sancarn opened 7 years ago

sancarn commented 7 years ago

If a VB Collection is created, e.g. a VB List:

Dim col as New List(Of String)
col.add(1)
col.add("some string")
col.add(1.2)

How do we parse the List in AHK?

sancarn commented 7 years ago
;Get ICollection from returned object (oRet)
ICollection := ComObject(ComObjType(oRet), ComObjQuery(oRet, "{DE8DB6F8-D101-3A92-8D1C-E72E5F10E992}"), 1)

;Get IList from ICollection
IList := ComObject(9, ComObjQuery(ICollection, "{7BCFA00F-F764-3113-9140-3BBD127A96BB}"), 1)

;Loop over the list and append them to an array
ret := []
Loop % ICollection.Count()
    ret.push(IList.Item(A_Index - 1))

With the addition of findIID() function this becomes a lot easier to manage (at the cost of speed):

getList(oRet){
    ;Get ICollection from returned object (oRet)
    ICollection := ComObject(ComObjType(oRet), ComObjQuery(oRet, findIID("ICollection")), 1)

    ;Get IList from ICollection
    IList := ComObject(9, ComObjQuery(ICollection, findIID("IList")), 1)

    ;Loop over the list and append them to an array
    ret := []
    Loop % ICollection.Count()
        ret.push(IList.Item(A_Index - 1))
    return ret
}

findIID(name){
    Loop, Reg, HKEY_CLASSES_ROOT\Interface\, KV
    {
        RegRead, interface, HKEY_CLASSES_ROOT\Interface\%A_LoopRegName%
        if (interface = name)
            return A_LoopRegName

    }
}
sancarn commented 7 years ago

Other IIDs:

Collection_Generic_Dictionary = {F31091E6-B0A8-11D6-B6FC-005056C00008}
IDispatch = {00020400-0000-0000-C000-000000000046}
IUnknown ={00000000-0000-0000-C000-000000000046}