这是正确的:Public Function AddVar(Dat() As Byte, Count As Long, ByVal Var As Variant)
Dim Ty As VbVarType
Dim Vint As Integer
Dim Vlng As Long
Dim Vsin As Single
Dim Vdub As DoubleTy = VarType(Var)If Ty = vbByte Then
ReDim Preserve Dat(Count)
Dat(Count) = Var
Count = Count + 1
ElseIf Ty = vbInteger Then
Vint = Var
ReDim Preserve Dat(Count + LenB(Vint) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vint), LenB(Vint)
Count = Count + LenB(Vint)
ElseIf Ty = vbVLong Then
Vlng = Var
ReDim Preserve Dat(Count + LenB(Vlng) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vlng), LenB(Vlng)
Count = Count + LenB(Vlng)
ElseIf Ty = vbSingle Then
Vsin = Var
ReDim Preserve Dat(Count + LenB(Vsin) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vsin), LenB(Vsin)
Count = Count + LenB(Vsin)
ElseIf Ty = vbDouble Then
Vdub = Var
ReDim Preserve Dat(Count + LenB(Vdub) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vdub), LenB(Vdub)
Count = Count + LenB(Vdub)
End IfEnd Function
下面这个是错误的Public Function AddVar(Dat() As Byte, Count As Long, ByVal Var As Variant)
Dim Ty As VbVarType
Dim Vint As Integer
Dim Vlng As Long
Dim Vsin As Single
Dim Vdub As DoubleTy = VarType(Var)If Ty = vbByte Then
ReDim Preserve Dat(Count)
Dat(Count) = Var
Count = Count + 1
ElseIf Ty = vbInteger Then
Vint = Var
ReDim Preserve Dat(Count + LenB(Vint) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vint, LenB(Vint)
Count = Count + LenB(Vint)
ElseIf Ty = vbVLong Then
Vlng = Var
ReDim Preserve Dat(Count + LenB(Vlng) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vlng, LenB(Vlng)
Count = Count + LenB(Vlng)
ElseIf Ty = vbSingle Then
Vsin = Var
ReDim Preserve Dat(Count + LenB(Vsin) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vsin, LenB(Vsin)
Count = Count + LenB(Vsin)
ElseIf Ty = vbDouble Then
Vdub = Var
ReDim Preserve Dat(Count + LenB(Vdub) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vdub, LenB(Vdub)
Count = Count + LenB(Vdub)
End IfEnd Function
这是两段代码唯一的不同为什么我加上了VarPtr就行呢????
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vlng), LenB(Vlng)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vlng, LenB(Vdub) 下面这段MoveMemory 是代码也是正确的,也没有加上VarPtr
MoveMemory U_CmdS(Index), U_CmdS(Index + 1), (U_Count - Index - 1) * Len(U_AutoCmd)
求指教谢谢
Dim Ty As VbVarType
Dim Vint As Integer
Dim Vlng As Long
Dim Vsin As Single
Dim Vdub As DoubleTy = VarType(Var)If Ty = vbByte Then
ReDim Preserve Dat(Count)
Dat(Count) = Var
Count = Count + 1
ElseIf Ty = vbInteger Then
Vint = Var
ReDim Preserve Dat(Count + LenB(Vint) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vint), LenB(Vint)
Count = Count + LenB(Vint)
ElseIf Ty = vbVLong Then
Vlng = Var
ReDim Preserve Dat(Count + LenB(Vlng) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vlng), LenB(Vlng)
Count = Count + LenB(Vlng)
ElseIf Ty = vbSingle Then
Vsin = Var
ReDim Preserve Dat(Count + LenB(Vsin) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vsin), LenB(Vsin)
Count = Count + LenB(Vsin)
ElseIf Ty = vbDouble Then
Vdub = Var
ReDim Preserve Dat(Count + LenB(Vdub) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vdub), LenB(Vdub)
Count = Count + LenB(Vdub)
End IfEnd Function
下面这个是错误的Public Function AddVar(Dat() As Byte, Count As Long, ByVal Var As Variant)
Dim Ty As VbVarType
Dim Vint As Integer
Dim Vlng As Long
Dim Vsin As Single
Dim Vdub As DoubleTy = VarType(Var)If Ty = vbByte Then
ReDim Preserve Dat(Count)
Dat(Count) = Var
Count = Count + 1
ElseIf Ty = vbInteger Then
Vint = Var
ReDim Preserve Dat(Count + LenB(Vint) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vint, LenB(Vint)
Count = Count + LenB(Vint)
ElseIf Ty = vbVLong Then
Vlng = Var
ReDim Preserve Dat(Count + LenB(Vlng) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vlng, LenB(Vlng)
Count = Count + LenB(Vlng)
ElseIf Ty = vbSingle Then
Vsin = Var
ReDim Preserve Dat(Count + LenB(Vsin) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vsin, LenB(Vsin)
Count = Count + LenB(Vsin)
ElseIf Ty = vbDouble Then
Vdub = Var
ReDim Preserve Dat(Count + LenB(Vdub) - 1)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vdub, LenB(Vdub)
Count = Count + LenB(Vdub)
End IfEnd Function
这是两段代码唯一的不同为什么我加上了VarPtr就行呢????
CopyMemory ByVal VarPtr(Dat(Count)), ByVal VarPtr(Vlng), LenB(Vlng)
CopyMemory ByVal VarPtr(Dat(Count)), ByVal Vlng, LenB(Vdub) 下面这段MoveMemory 是代码也是正确的,也没有加上VarPtr
MoveMemory U_CmdS(Index), U_CmdS(Index + 1), (U_Count - Index - 1) * Len(U_AutoCmd)
求指教谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货