VBA数据类型Array

2020-07-28 10:16:08 浏览数 (1)

1、VBA数组底层结构:

VBA的数组在底层是SafeArray:

代码语言:javascript复制
'https://docs.microsoft.com/zh-cn/windows/win32/api/oaidl/ns-oaidl-safearraybound?redirectedfrom=MSDN
Type SafeArrayBound
    cElements As Long '// 该维的长度
    lLbound   As Long ' // 该维的数组存取的下限,一般为0
End Type
'https://docs.microsoft.com/zh-cn/windows/win32/api/oaidl/ns-oaidl-safearray?redirectedfrom=MSDN
Type SafeArray
    cDims      As Integer ' // 数组的维度
    fFeatures  As Integer '
    cbElements As Long ' // 数组元素的字节大小
    cLocksas   As Long '
    pvDataas   As Long ' // 数组的数据指针
    rgsabound(0) As SafeArrayBound
End Type

如果要取数组的地址,需要用到API函数:

代码语言:javascript复制
Public Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef Var() As Any) As Long

VarPtrArray 返回的并不是SafeArray的地址,我们可以进行测试:

代码语言:javascript复制
Sub TestArray()
    Dim Arr() As Byte
    
    ReDim Arr(3) As Byte
    
    Dim sa As SafeArray
    CopyMemory VarPtr(sa), VarPtrArray(Arr), Len(sa)
    
    Printf "sa.cDims = %d, sa.cbElements = %d", sa.cDims, sa.cbElements
End Sub
输出:
sa.cDims = -27936, sa.cbElements = 3994212

从输出可以看出,我们预计的sa.cDims应该=1和sa.cbElements=1并没有出现,显然VarPtrArray(Arr)获取到的还不是Arr的地址。

像c语言这样的语言,是有指针的,VBA数组的底层实现应该是使用了一个指针来引用SafeArray结构,而VarPtrArray(Arr)获取到的应该是指针的地址。

代码语言:javascript复制
Sub TestArray()
    Dim Arr() As Byte
    ReDim Arr(3) As Byte
    Dim ptr As Long '保存[Arr指针]的地址
    
    CopyMemory VarPtr(ptr), VarPtrArray(Arr), 4
    
    Dim sa As SafeArray
    CopyMemory VarPtr(sa), ptr, Len(sa)
    
    Printf "sa.cDims = %d, sa.cbElements = %d", sa.cDims, sa.cbElements

End Sub
输出
sa.cDims = 1, sa.cbElements = 1

从输出可以看出,预计的sa.cDims应该=1和sa.cbElements=1出现了。

进一步测试,从pvDataas 中提取数据,看看获取的数据是否能正确:

代码语言:javascript复制
Sub TestArray2()
    Dim Arr() As Byte
    ReDim Arr(3) As Byte
    Dim ptr As Long '保存[Arr指针]的地址
    
    Arr(0) = &H88
    Arr(1) = &H21
    Arr(2) = &H27
    Arr(3) = &H99
    
    CopyMemory VarPtr(ptr), VarPtrArray(Arr), 4
    
    Dim sa As SafeArray
    CopyMemory VarPtr(sa), ptr, Len(sa)
    
    Dim b(3) As Byte
    Dim i As Long
    For i = 0 To 3
        CopyMemory VarPtr(b(i)), sa.pvDataas   sa.cbElements * i, sa.cbElements
        Printf "b(%d) = 0x%x ", i, b(i)
    Next
End Sub
输出:
b(0) = 0x88 
b(1) = 0x21 
b(2) = 0x27 
b(3) = 0x99

完全正确。

2、改变SafeArray的pvDataas地址会有什么情况:

既然知道了数组的内存结构,那我们就尝试把pvDataas改变看看会怎么样:

代码语言:javascript复制
Sub TestArray2()
    Dim Arr() As Byte
    ReDim Arr(0) As Byte
    Dim ptr As Long '保存[Arr指针]的地址
    
    CopyMemory VarPtr(ptr), VarPtrArray(Arr), 4
    
    Dim lValue As Long
    lValue = &HABCDEF99
    
    Dim plValue As Long
    plValue = VarPtr(lValue)
    
    '修改pvDataas指向lValue
    CopyMemory ptr   12, VarPtr(plValue), 4
    '修改Arr的SafeArrayBound为4
    Dim cElements As Long
    cElements = 4
    CopyMemory ptr   16, VarPtr(cElements), 4
    
    Dim sa As SafeArray
    CopyMemory VarPtr(sa), ptr, Len(sa)
    
    Printf "sa.pvDataas = 0x%x, plValue = 0x%x", sa.pvDataas, plValue
    
    Dim i As Long
    For i = 0 To 3
        Printf "Arr(%d) = 0x%x ", i, Arr(i)
    Next
End Sub
输出:
sa.pvDataas = 0x36ecbc, plValue = 0x36ecbc
Arr(0) = 0x99 
Arr(1) = 0xef 
Arr(2) = 0xcd 
Arr(3) = 0xab 

定义了一个容量为1的byte数组,但是我把pvDataas的值修改成了1个Long变量的地址,同时也改变了他的SafeArrayBound为4,从输出可以看到,这个数组已经变成了一个容量为4的byte数组。

但是End Sub后,我的电脑测试Excel崩溃,这个和前面讲到的String类型里的情况差不多,到底是什么原因?

3、ReDim Preserve做了什么

我们经常会用ReDim Preserve来改变数组的容量,当然一般都是扩大。扩大的话要更多的内存空间来保存数据,所以应该是要重新分配内存,测试:

代码语言:javascript复制
Sub TestArray()
    Dim Arr() As Byte
    ReDim Arr(3) As Byte
    Dim ptr As Long '保存[Arr指针]的地址
    Dim sa As SafeArray
    
    CopyMemory VarPtr(ptr), VarPtrArray(Arr), 4
    CopyMemory VarPtr(sa), ptr, Len(sa)
    Printf "ptr = 0x%x, sa.pvDataas= 0x%x, sa.cDims = %d, sa.cbElements = %d, sa.rgsabound(0).cElements = %d", ptr, sa.pvDataas, sa.cDims, sa.cbElements, sa.rgsabound(0).cElements

    ReDim Preserve Arr(4) As Byte
    CopyMemory VarPtr(ptr), VarPtrArray(Arr), 4
    CopyMemory VarPtr(sa), ptr, Len(sa)
    Printf "ptr = 0x%x, sa.pvDataas= 0x%x, sa.cDims = %d, sa.cbElements = %d, sa.rgsabound(0).cElements = %d", ptr, sa.pvDataas, sa.cDims, sa.cbElements, sa.rgsabound(0).cElements
End Sub
输出:
ptr = 0x16597108, sa.pvDataas= 0x167c3a40, sa.cDims = 1, sa.cbElements = 1, sa.rgsabound(0).cElements = 4
ptr = 0x16597108, sa.pvDataas= 0x167c3790, sa.cDims = 1, sa.cbElements = 1, sa.rgsabound(0).cElements = 5

pvDataas已经改变,重新分配了内存空间。

但是,如果是减小呢?

我本来猜测只要修改sa.rgsabound(0).cElements的值就可以达到减小容量的目的,可测试却同样重新分配了内存:

代码语言:javascript复制
Sub TestArray()
    Dim Arr() As Byte
    ReDim Arr(3) As Byte
    Dim ptr As Long '保存[Arr指针]的地址
    Dim sa As SafeArray
    
    CopyMemory VarPtr(ptr), VarPtrArray(Arr), 4
    CopyMemory VarPtr(sa), ptr, Len(sa)
    Printf "ptr = 0x%x, sa.pvDataas= 0x%x, sa.cDims = %d, sa.cbElements = %d, sa.rgsabound(0).cElements = %d", ptr, sa.pvDataas, sa.cDims, sa.cbElements, sa.rgsabound(0).cElements

    ReDim Preserve Arr(1) As Byte
    CopyMemory VarPtr(ptr), VarPtrArray(Arr), 4
    CopyMemory VarPtr(sa), ptr, Len(sa)
    Printf "ptr = 0x%x, sa.pvDataas= 0x%x, sa.cDims = %d, sa.cbElements = %d, sa.rgsabound(0).cElements = %d", ptr, sa.pvDataas, sa.cDims, sa.cbElements, sa.rgsabound(0).cElements
End Sub
输出:
ptr = 0x167d5068, sa.pvDataas= 0x167c3a50, sa.cDims = 1, sa.cbElements = 1, sa.rgsabound(0).cElements = 4
ptr = 0x167d5068, sa.pvDataas= 0x167c37f0, sa.cDims = 1, sa.cbElements = 1, sa.rgsabound(0).cElements = 2

0 人点赞