新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim arrA(10) As Byte
Dim arrB(10) As Byte
Dim i As Long
Dim blSame As Boolean
arrA(2) = 2 '设置一个不为0
CopyMemory arrB(0), arrA(0), UBound(arrA) + 1 ' 高速复制arrA的内容到arrB
Debug.Print arrB(2)
arrB(2) = 3
blSame = True
For i = 0 To UBound(arrA)
If arrA(i) <> arrB(i) Then blSame = False: Exit For
Next
If blSame = True Then
Debug.Print "相同"
Else
Debug.Print "不同"
End If
End Sub
结果
2
不同
不过,CopyMemory可能会内存程序崩溃出错,要注意哦。
下表总结了几种常见的传参数给CopyMemory的形式:除了字节和StrPtr都是4。