首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Kernel32中编写Copymem Lib "Kernel32“别名"Rtlmovememory”

如何在Kernel32中编写Copymem Lib "Kernel32“别名"Rtlmovememory”
EN

Stack Overflow用户
提问于 2015-02-04 09:42:43
回答 1查看 4.2K关注 0票数 0

下面是我的vb6代码

代码语言:javascript
复制
    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代码

代码语言:javascript
复制
  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的代码

代码语言:javascript
复制
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

我参考了CopyMemory在.NET中的等价性

但我还是不明白

谁来帮帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-04 10:15:29

如果希望使用.NET中的指针访问数据,则需要在整个操作期间将它们固定在一起。VarPtr方法在获取该对象的地址时将其引脚,但随后它将该对象解压缩。这意味着在执行CopyMem调用时可以移动对象。大多数情况下,对象没有被移动,所以它看起来工作得很好,但是当它被移动时,CopyMem操作可能会更改其他一些数据。这可能会使应用程序中的任何对象行为怪异,或使应用程序崩溃。

无论如何,使用内存复制对于移动整数中的几个位绝对是过分的。(顺便说一下,VB 6中的Long数据类型与VB.NET中的Integer数据类型相对应。)

可以将整数转换为字节数组,使用Array.Copy方法,然后将其转换回:

代码语言:javascript
复制
Dim temp As Byte() = BitConverter.GetBytes(dataX)
Array.Copy(temp, 0, temp, 1, 3)
dataX = BitConverter.ToInt32(temp, 0)

您还可以使用位操作来完成此操作:

代码语言:javascript
复制
dataX = (dataX And &HFF) Or (dataX << 8)

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28318121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档