Как устранить ошибку копирования памяти в VB.NET?
Я преобразовал нижеприведенную кодовую форму vb6 в vb.net. Тот же код отлично работает в VB6, но в VB.net я получаю ошибку "
Attempted to read or write protected memory. This is often an indication that other memory is corrupt
"
Декларация для CopyMemory является
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Long, Source As Long, ByVal Length As Long) Public Function EncodeArray64(ByRef bytInput() As Byte) As String On Error GoTo ErrorHandler If m_bytReverseIndex(47) <> 63 Then Initialize64() Dim bytWorkspace() As Byte, bytResult() As Byte Dim bytCrLf(0 To 3) As Byte, lCounter As Long Dim lWorkspaceCounter As Long, lLineCounter As Long Dim lCompleteLines As Long, lBytesRemaining As Long Dim lpWorkSpace As Long, lpResult As Long Dim lpCrLf As Long If UBound(bytInput) < 1024 Then ReDim bytWorkspace(0 To (LBound(bytInput) + 4096)) Else ReDim bytWorkspace(0 To (UBound(bytInput) * 4)) End If lWorkspaceCounter = LBound(bytWorkspace) For lCounter = LBound(bytInput) To (UBound(bytInput) - ((UBound(bytInput) Mod 3) + 3)) Step 3 bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) \ k_bytShift2)) bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytMask1) * k_bytShift4) + ((bytInput(lCounter + 1)) \ k_bytShift4)) bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex(((bytInput(lCounter + 1) And k_bytMask2) * k_bytShift2) + (bytInput(lCounter + 2) \ k_bytShift6)) bytWorkspace(lWorkspaceCounter + 6) = m_bytIndex(bytInput(lCounter + 2) And k_bytMask3) lWorkspaceCounter = lWorkspaceCounter + 8 Next lCounter Select Case (UBound(bytInput) Mod 3) Case 0 bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) \ k_bytShift2)) bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex((bytInput(lCounter) And k_bytMask1) * k_bytShift4) bytWorkspace(lWorkspaceCounter + 4) = k_bytEqualSign bytWorkspace(lWorkspaceCounter + 6) = k_bytEqualSign Case 1 bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) \ k_bytShift2)) bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytMask1) * k_bytShift4) + ((bytInput(lCounter + 1)) \ k_bytShift4)) bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex((bytInput(lCounter + 1) And k_bytMask2) * k_bytShift2) bytWorkspace(lWorkspaceCounter + 6) = k_bytEqualSign Case 2 bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) \ k_bytShift2)) bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytMask1) * k_bytShift4) + ((bytInput(lCounter + 1)) \ k_bytShift4)) bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex(((bytInput(lCounter + 1) And k_bytMask2) * k_bytShift2) + ((bytInput(lCounter + 2)) \ k_bytShift6)) bytWorkspace(lWorkspaceCounter + 6) = m_bytIndex(bytInput(lCounter + 2) And k_bytMask3) End Select lWorkspaceCounter = lWorkspaceCounter + 8 If lWorkspaceCounter <= k_lMaxBytesPerLine Then EncodeArray64 = Left(bytWorkspace.ToString, InStr(1, bytWorkspace.ToString, "") - 1) Else bytCrLf(0) = 13 bytCrLf(1) = 0 bytCrLf(2) = 10 bytCrLf(3) = 0 ReDim bytResult(0 To UBound(bytWorkspace)) lpWorkSpace = VarPtr.VarPtr(bytWorkspace(LBound(bytWorkspace))) lpResult = VarPtr.VarPtr(bytResult(LBound(bytResult))) lpCrLf = VarPtr.VarPtr(bytCrLf(LBound(bytCrLf))) lCompleteLines = Fix(lWorkspaceCounter / k_lMaxBytesPerLine) For lLineCounter = 0 To lCompleteLines CopyMemory(lpResult, lpWorkSpace, k_lMaxBytesPerLine) lpWorkSpace = lpWorkSpace + k_lMaxBytesPerLine lpResult = lpResult + k_lMaxBytesPerLine CopyMemory(lpResult, lpCrLf, 4&) lpResult = lpResult + 4& Next lLineCounter lBytesRemaining = lWorkspaceCounter - (lCompleteLines * k_lMaxBytesPerLine) If lBytesRemaining > 0 Then CopyMemory(lpResult, lpWorkSpace, lBytesRemaining) EncodeArray64 = Left(bytResult.ToString, InStr(1, bytResult.ToString, "") - 1) End If Exit Function ErrorHandler: Erase bytResult EncodeArray64 = bytResult.ToString End Function
Что я уже пробовал:
Я изменил объявление API, как показано ниже, но оно не работает
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Long, ByRef Source As Long, ByVal Length As Integer)