如何编写 Copymem Lib "Kernel32" Alias "Rtlmovememory" In Vb.Net

How To I Write Copymem Lib "Kernel32" Alias "Rtlmovememory" In Vb.Net

下面是我的 vb6 代码

    Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)


Public Property Let Key(New_Value As String)

  Dim i As Long
  Dim j As Long
  Dim K As Long
  Dim dataX As Long
  Dim datal As Long
  Dim datar As Long
  Dim Key() As Byte
  Dim KeyLength As Long

  'Do nothing if the key is buffered
  If (m_KeyValue = New_Value) Then Exit Property
  m_KeyValue = New_Value

  'Convert the new key into a bytearray
  KeyLength = Len(New_Value)
  Key() = StrConv(New_Value, vbFromUnicode)

  'Create key-dependant p-boxes
  j = 0
  For i = 0 To (ROUNDS + 1)
    dataX = 0
    For K = 0 To 3
      Call CopyMem(ByVal VarPtr(dataX) + 1, dataX, 3) 'the problem is here
      dataX = (dataX Or Key(j))
      j = j + 1
      If (j >= KeyLength) Then j = 0
    Next
    m_pBox(i) = m_pBox(i) Xor dataX
  Next

End Property

CopyMem 子库如何在 vb.net

中使用

现在这是我的 vb.net 相同代码

  Private Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" (ByVal pDst As Object, ByVal pSrc As Object, ByVal ByteLen As Integer)


 Public WriteOnly Property Key() As String
        Set(ByVal Value As String)

            Dim i As Long
            Dim j As Long
            Dim K As Long
            Dim dataX As Long
            Dim datal As Long
            Dim datar As Long
            Dim Key() As Byte
            Dim KeyLength As Long

            'Do nothing if the key is buffered
            If (m_KeyValue = Value) Then Exit Property
            m_KeyValue = Value

            'Convert the new key into a bytearray
            KeyLength = Len(Value)

            Key = System.Text.Encoding.Unicode.GetBytes(Value)

            'Create key-dependant p-boxes
            j = 0

            For i = 0 To (ROUNDS + 1)
                dataX = 0
                For K = 0 To 3


                    CopyMem(VarPtr(dataX) + 1, dataX, 3) ' the problem is here
                    dataX = (dataX Or Key(j))
                    j = j + 1
                    If (j >= KeyLength) Then j = 0

                Next
                m_pBox(i) = m_pBox(i) Xor dataX
            Next
 End Property

这是 VarPtr

的代码
Public Function VarPtr(ByVal e As Object) As Object
        Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
        Dim GC2 As Integer = GC.AddrOfPinnedObject.ToInt32
        GC.Free()
        Return GC2
    End Function

我参考了Equivalent of CopyMemory in .NET

但我还是没听懂

请有人帮忙!!!

如果您想在 .NET 中使用指针访问数据,那么您需要在整个操作期间将它们固定。 VarPtr 方法在获取对象地址时固定对象,但随后取消固定对象。这意味着可以在执行 CopyMem 调用时移动对象。大多数情况下对象不会被移动,所以它看起来工作正常,但是当它被移动时 CopyMem 操作可能会改变一些其他数据。这可能会使应用程序中的任何对象行为异常,或使应用程序崩溃。

无论如何,使用内存复制对于移动整数中的几个位肯定是大材小用。 (VB6中的Long数据类型顺便对应了VB.NET中的Integer数据类型。)

可以把整数转成字节数组,用Array.Copy方法,再转回来:

Dim temp As Byte() = BitConverter.GetBytes(dataX)
Array.Copy(temp, 0, temp, 1, 3)
dataX = BitConverter.ToInt32(temp, 0)

您也可以使用位运算来完成:

dataX = (dataX And &HFF) Or (dataX << 8)

旁注:Encoding.Unicode 用于 UTF-16 编码。这意味着 GetBytes returns 的字节数组将是字符串长度的两倍,因此您将只使用字符串的一半。