Skip to content

Issues of stdRefArray #145

Description

@leolee82
  1. VarPtrArr shall be reurn the pointer of the safearray address.
#If Win64 Then
Private Function VarPtrArr(ByRef arr As Variant) As LongLong
#Else
Private Function VarPtrArr(ByRef arr As Variant) As Long
#End If

Dim vt As VbVarType
Dim p As Long
p = VarPtr(arr)
CopyMemory vt, ByVal p, 2

If (vt And vbArray) = vbArray Then '//安全数组
    VarPtrArr = PtrAdd(p, 8) 'VARIANT结构中wReserved2的地址
    
    If (vt And &H4000) = &H4000 Then     '//引用类型VT_BYREF,如果是引用类型则wReserved2的值才是数组地址的地址
        CopyMemory VarPtrArr, ByVal VarPtrArr, PTR_SIZE
    End If
End If
End Function
Private Function PtrAdd(ByVal Ptr As LongPtr, ByVal iOffset As Long) As LongPtr
    ' For adding (or subtracting) a small number from a pointer.
    ' Use PtrAddEx for adding (or subtracting) large numbers from a pointer.
    PtrAdd = (Ptr Xor &H80000000) + iOffset Xor &H80000000
End Function
  1. It is better to provide a default property to use it like a real array. But there is some CopyMemory cost that it shall not be used in a loop
Property Get GetData()
Attribute GetData.VB_UserMemId = 0
Call CopyMemory(GetData, data, 16)
End Property

Sub TeststdRefArray2()
Dim arr As Variant: arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Dim rarr As stdRefArray: Set rarr = stdRefArray.Create(arr)
Debug.Print rarr(1)  '=> 2
rarr(1) = 3
Debug.Print rarr(1)  '=> 3
Debug.Print rarr(2)  '=> 3
End Sub

Sub TeststdRefArray3()
Dim arr As Variant: arr = [M1:M3]

Dim rarr As stdRefArray: Set rarr = stdRefArray.Create(arr)
Debug.Print rarr(1, 1) '=> 2
rarr(1, 1) = 3
Debug.Print rarr(1, 1) '=> 3
Debug.Print rarr(2, 1) '=> 3
End Sub

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions