首页 /编程语言和算法/VB6/ASP
 VB6 快速复制字节数组 WinAPI 函数CopyMemory 和 字节比较
1月16日 20:15

新建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。

111.jpg

 
全部回复(0)
首页 | 电脑版 |