程序升级,VB6转VB2008,求COPYMEMORY的替换代码,或直接将COPYMEMORY直接用到VB2008中的解决方法。通过COPYMEMORY实现的功能,主要是实现以下功能:
1、将数组(256字节)中的值存放到相应的整形、长整型、日期型、字符型变量中。
2、将整形、长整型、日期型、字符型变量的值存放到数组(256字节)中。以下是详细代码:Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Buffer(256) As Byte         '与卡交换缓冲区Private Sub BufferInit()
    Dim i As Integer
    For i = 0 To 255
        Buffer(i) = 0
    Next
End SubPrivate Sub WriteBufferFromByte(FirstPoint As Integer, value As Byte)
    CopyMemory Buffer(FirstPoint), value, 1
End Sub
Private Sub WriteBufferFromDouble(FirstPoint As Integer, value As Double)
    CopyMemory Buffer(FirstPoint), value, 8
End Sub
Private Sub WriteBufferFromDate(FirstPoint As Integer, value As Date)
    CopyMemory Buffer(FirstPoint), value, 8
End SubPrivate Sub WriteBufferFromLong(FirstPoint As Integer, value As Long, Length As Integer)
    Dim myBuff(4) As Byte
    CopyMemory myBuff(0), value, 4
    If Length = 1 Then
        CopyMemory Buffer(FirstPoint), myBuff(0), 1
    End If
    If Length = 2 Then
        CopyMemory Buffer(FirstPoint), myBuff(0), 2
    End If
    If Length = 4 Then
        CopyMemory Buffer(FirstPoint), myBuff(0), 4
    End If
End SubPrivate Sub WriteBufferFromString(FirstPoint As Integer, value As String)
    Dim strbyte() As Byte
    strbyte = StrConv(value, vbFromUnicode)
    CopyMemory Buffer(FirstPoint), strbyte(0), Len(value) * 2
End SubPrivate Sub ReadBufferToByte(FirstPoint As Integer, value As Byte)
    CopyMemory value, Buffer(FirstPoint), 1
End SubPrivate Sub ReadBufferToDouble(FirstPoint As Integer, value As Double)
    CopyMemory value, Buffer(FirstPoint), 8
End SubPrivate Sub ReadBufferToDate(FirstPoint As Integer, value As Date)
    CopyMemory value, Buffer(FirstPoint), 8
End SubPrivate Sub ReadBufferToLong(FirstPoint As Integer, value As Long, Length As Integer)
    Dim myBuff(4) As Byte
    If Length = 1 Then
        CopyMemory myBuff(0), Buffer(FirstPoint), 1
        myBuff(1) = 0
        myBuff(2) = 0
        myBuff(3) = 0
    End If
    If Length = 2 Then
        CopyMemory myBuff(0), Buffer(FirstPoint), 2
        myBuff(2) = 0
        myBuff(3) = 0
    End If
    If Length = 4 Then
        CopyMemory myBuff(1), Buffer(FirstPoint), 4
    End If
    CopyMemory value, myBuff(0), 4
End Sub
Private Sub ReadBufferToString(FirstPoint As Integer, value As String, iLength As Integer)
    Dim strbyte(64) As Byte
    Dim TempStr As String
    CopyMemory strbyte(0), Buffer(FirstPoint), iLength * 2
    TempStr = StrConv(strbyte, vbUnicode)
    value = Mid(TempStr, 1, iLength)
    'value = strbyte
End Sub

解决方案 »

  1.   

    Runtime.InteropServices.Marshal.Copy()
      

  2.   

    .NET封装了内存拷贝方法,可以直接调用Marshal.Copy
    vb6和net可以说是二种不同的东西,既然你要学net,好好看看MSDN....
      

  3.   

    大哥,除了MSDN,就没有书可看了吗?我一看MSDN就头大。
      

  4.   

    VB.NET支持API呀。你可以将CopyMemory分开为String类型,Byte类型和自定义Structure类型。
    我有实践经验。也已经成功转化过几个项目。
    例如String类型:
    Private Declare Sub CopyMemory_FromString Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As Integer, ByVal pSrc As String, ByVal ByteLen As Integer)Private Declare Sub CopyMemory_ToString Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As String, ByVal pSrc As Integer, ByVal ByteLen As Integer)
      

  5.   

    VBAdvisor,给你发的站内短消息收到了么?
      

  6.   

    imports System
    imports System.Text public module MyModule
    Private BufferArr(255) As Byte

    sub Main
    try
        WriteBufferFromByte(0, &H10)
    WriteBufferFromDate(1, #11/4/2008#)
    WriteBufferFromDouble(9, 0.1)
    WriteBufferFromLong(17, &H12345678, 4)
    WriteBufferFromString(21, "abc中文 ")
    Console.WriteLine("BufferArr = {0}",BitConverter.ToString(BufferArr))

    Dim bt As Byte, dt As Date, dbl As Double, l As Long, s As String=""
    ReadBufferToByte(0, bt)
    ReadBufferToDate(1, dt)
    ReadBufferToDouble(9, dbl)
    ReadBufferToLong(17, l, 4)
    ReadBufferToString(21, s, 6)
    Console.WriteLine("{0:X2}; {1}; {2}; {3:X8}; {4};", bt, dt, dbl, l, s)

    catch e as System.Exception 
    Console.WriteLine(e.Message)
    end try
    Console.ReadLine()
    end sub Private Sub BufferInit()
    Redim BufferArr(256)
    End Sub

    Private Sub WriteBufferFromByte(FirstPoint As Integer, value As Byte)
    BufferArr(FirstPoint) = value
    End Sub Private Sub WriteBufferFromDouble(FirstPoint As Integer, value As Double)
    dim tmpArr() as byte =BitConverter.GetBytes(value)
    Array.Copy(tmpArr,0,BufferArr,FirstPoint,tmpArr.Length)
    End Sub
    Private Sub WriteBufferFromDate(FirstPoint As Integer, value As Date)
    dim OADate as double = value.ToOADate()
    WriteBufferFromDouble(FirstPoint, OADate)
    End Sub

    Private Sub WriteBufferFromLong(FirstPoint As Integer, value As Long, Length As Integer)
    dim tmpArr() as byte =BitConverter.GetBytes(value)
    Array.Copy(tmpArr,0,BufferArr,FirstPoint,Length)
    End Sub

    Private Sub WriteBufferFromString(FirstPoint As Integer, value As String)
    dim e as Encoding =System.Text.Encoding.Default
    Dim tmpArr() As Byte = e.GetBytes(value)
    Array.Copy(tmpArr,0,BufferArr,FirstPoint,tmpArr.Length)
    End Sub Private Sub ReadBufferToByte(FirstPoint As Integer, ByRef value As Byte)
    value = BufferArr(FirstPoint)
    End Sub

    Private Sub ReadBufferToDouble(FirstPoint As Integer, ByRef value As Double)
    value = BitConverter.ToDouble(BufferArr,FirstPoint)
    End Sub

    Private Sub ReadBufferToDate(FirstPoint As Integer, ByRef value As Date)
    dim OADate as double = BitConverter.ToDouble(BufferArr,FirstPoint)
    value = DateTime.FromOADate(OADate)
    End Sub

    Private Sub ReadBufferToLong(FirstPoint As Integer, ByRef value As integer, Length As Integer)
    Dim tmpArr(3) As Byte
    Array.Copy(BufferArr,FirstPoint,tmpArr,0,Length)
    value = BitConverter.ToInt32(tmpArr,0)
    End Sub

    Private Sub ReadBufferToString(FirstPoint As Integer, ByRef value As String, iLength As Integer)
    dim e as Encoding =System.Text.Encoding.Default
    Dim tmpStr As String = e.GetChars(BufferArr,FirstPoint,iLength*2)
    value = tmpStr.SubString(0,iLength)
    End Sub
    end module
      

  7.   

    更正:Redim 的参数也应该是 255
      

  8.   

    CopyMemory的参数不可以再定义为Any型,VB6的Long改为VB.NET的Integer型。并且分开处理:String类型,Byte类型和自定义Structure类型。
     
    String类型:Private Declare Sub CopyMemory_FromString Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As Integer, ByVal pSrc As String, ByVal ByteLen As Integer)
    Private Declare Sub CopyMemory_ToString Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As String, ByVal pSrc As Integer, ByVal ByteLen As Integer)
    Byte类型:Private Declare Sub CopyMemory_FromByte Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As Integer, ByRef pSrc As Byte, ByVal ByteLen As Integer)
    Private Declare Sub CopyMemory_ToByte Lib "kernel32" Alias "RtlMoveMemory" (ByRef pDst As Byte, ByVal pSrc As Integer, ByVal ByteLen As Integer)自定义结构(Structure):Private Declare Sub CopyMemory_FromMyFormat Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As Integer, ByRef pSrc As MyFormat, ByVal ByteLen As Integer)
    Private Declare Sub CopyMemory_ToMyFormat Lib "kernel32" Alias "RtlMoveMemory" (ByRef pDst As MyFormat, ByVal pSrc As Integer, ByVal ByteLen As Integer)
      

  9.   

    关于学习 .Net:
    建议浏览一遍 MSDN 中的类库参考,你会发现许多原先需要调用 API 的功能现在都包含在类库中。
    对某个类的描述不理解时,还可以用 Reflector 这个工具直接看该类的源代码。