查看: 169  |  回复: 0
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

您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2024 MaNongKu.com