如何在VB.NET中解决复制内存错误? [英] How to resolve copy memory error in VB.NET?

查看:100
本文介绍了如何在VB.NET中解决复制内存错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将以下代码格式vb6转换为vb.net。相同的代码在VB6中工作正常但在VB.net中我收到错误

I have converted the below code form vb6 to vb.net. Same code is working fine in VB6 but in VB.net i am getting error "

Attempted to read or write protected memory. This is often an indication that other memory is corrupt







CopyMemory的声明是



"

Declaration for CopyMemory is

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声明如下,但它不起作用





What I have tried:

I changed the API declaration as below, but it is not working

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Long, ByRef Source As Long, ByVal Length As Integer)

推荐答案

好的,我可以问一个问题(不,做那两个:)自从我已经问过一个问题了......第二个问题是'为什么' - 你为什么要重新编写从VB6到VB.Net的Base64转换例程而不是使用.Net内置的东西?



什么是错的



ok, can I ask a question (no, make that two :) since Ive asked one already ) .. the second of which is 'why' - why would you re-write a Base64 conversion routine from VB6 to VB.Net and not use things built into .Net instead ?

Whats wrong with

System.Convert.ToBase64String
System.Convert.FromBase64String





我能理解从VB6到VB.Net,但是,当然这个过程将是'优化并将尽可能多的东西转换成VB.Net中的新方式而不是盲目复制一切



- 你真的会有很多东西要做的工作,说服我在VB.Net中从头开始重写Base64转换并修复,正如你所发现的,处理内存的不同方法,值得付出努力/时间



(对不起,如果听起来有点苛刻 - 这不是那种意思)



I can understand going from VB6 to VB.Net, but, surely that process would be 'optimise and convert as many things as possible to to 'the new way' in VB.Net' as opposed to 'blindly replicate everything'

- you would honestly have a lot of work to do, to convince me that rewriting a Base64 convert from scratch in VB.Net and fixing the, as you've discovered, different approach to handling memory, would be worth the effort/time

(sorry if this sounds a little harsh - its not meant that way)


这篇关于如何在VB.NET中解决复制内存错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆