Socket接收到的byte []要转换成自定义的struct / 自定义Struct转换成byte []都相当麻烦 用循环去转换太浪费时间了……于是想到用CopyMemory,Google一圈终于搞定 kernel32.dll", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi)] public extern static long CopyMemory dp = (IntPtr)ds; sp = (IntPtr)sr; return CopyMemory dp = (IntPtr)ds; sp = (IntPtr)sr; return CopyMemory dp = (IntPtr)ds; sp = (IntPtr)sr; return CopyMemory
public ByteBuf setBytes(int index, ByteBuf src, int length) {
Sub TestArray() Dim Arr() As Byte ReDim Arr(3) As Byte Dim sa As SafeArray CopyMemory VarPtr(ptr), VarPtrArray(Arr), 4 Dim sa As SafeArray CopyMemory VarPtr(sa), ptr, Len(sa VarPtr(ptr), VarPtrArray(Arr), 4 Dim sa As SafeArray CopyMemory VarPtr(sa), ptr, Len(sa ) Dim b(3) As Byte Dim i As Long For i = 0 To 3 CopyMemory VarPtr(b(i)), sa.pvDataas ptr + 16, VarPtr(cElements), 4 Dim sa As SafeArray CopyMemory VarPtr(sa), ptr, Len(sa)
就是指明数组维度的,那么,我们只需要通过修改内存中cDims的值,以及SafeArray中rgsabound记录的元素的个数,那么就可以实现将多维的数组转换为一维数组: 代码: Public Declare Sub CopyMemory ptr, VarPtr(sa), 16 CopyMemory ptr + 16, VarPtr(sa.rgsabound(0).cElements), 8 End Function Function MyArrayPtr(ByRef v As Variant) As Long Dim b(16 - 1) As Byte CopyMemory VarPtr(b(0)), VarPtr (v), 16 ' Printf "b = 0x% x", b Dim ptr As Long CopyMemory VarPtr(ptr), VarPtr(b(8)), 4 ' - 0x20 8-11存的是数组地址 ' - 0x60 8-11存的是数组地址的地址 If b(1) = &H60 Then CopyMemory VarPtr
End Function 在数据类型Array中,我们知道了数组的底层结构,其中cDims就是指明数组维度的,那么,我们只需要读取到cDims的值就可以了: Public Declare Sub CopyMemory Function End If Dim ptr As Long Dim sa As SafeArray ptr = MyArrayPtr(v) CopyMemory sa.cDims End Function Function MyArrayPtr(ByRef v As Variant) As Long Dim b(16 - 1) As Byte CopyMemory VarPtr(b(0)), VarPtr(v), 16 Dim ptr As Long CopyMemory VarPtr(ptr), VarPtr(b(8)), 4 ' - 0x20 8-11存的是数组地址 ' - 0x60 8-11存的是数组地址的地址 If b(1) = &H60 Then CopyMemory VarPtr(ptr),
p4 = VarPtrArray(a4) Dim p1value As Long, p2value As Long, p3value As Long, p4value As Long CopyMemory VarPtr(p1value), p1, 4 CopyMemory VarPtr(p2value), p2, 4 CopyMemory VarPtr(p3value), p3, 4 CopyMemory VarPtr(p4value), p4, 4 Debug.Print p1, p1value Debug.Print p2, p2value Debug.Print VarPtr(p1value), p1, 4 Dim sa As SafeArray '获取SafeArrayBound之前的数据 CopyMemory VarPtr(sa.cDims ), p1value, 16 ReDim sa.rgsabound(sa.cDims - 1) As SafeArrayBound '根据维度再读取需要的数据 CopyMemory
TestMyArrayPtr() Dim Arr() As Byte ReDim Arr(3) As Byte Dim ptr As Long '保存[Arr指针]的地址 CopyMemory MyArrayPtr(Arr) End Sub Function MyArrayPtr(ByRef v As Variant) As Long Dim b(16 - 1) As Byte CopyMemory VarPtr(b(0)), VarPtr(v), 16 Printf "b = 0x% x", b Dim ptr As Long CopyMemory VarPtr VarPtr(b(8)), 4 ' - 0x20 8-11存的是数组地址 ' - 0x60 8-11存的是数组地址的地址 If b(1) = &H60 Then CopyMemory
Sub TestString() Dim str As String Dim lVarPtr As Long str = "a" CopyMemory lVarPtr End Sub 输出: VarPtr(str) = 0x1ef030, StrPtr(str) = 0x17a455ec, lVarPtr = 0x17a455ec 说明: API CopyMemory 声明 Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal printf "VarPtr(l) = 0x%x, StrPtr(l) = 0x%x", VarPtr(l), StrPtr(l) Dim b(3) As Byte CopyMemory obj) Sub TestObjPtr() Dim Rng As Range Dim lVarPtr As Long Set Rng = Range("A1") CopyMemory
= i Dim lenth As Long lenth = 16 Dim b() As Byte ReDim b(lenth - 1) As Byte CopyMemory lenth As Long lenth = 16 Dim b() As Byte ReDim b(lenth - 1) As Byte CopyMemory VarPtr(b(0)), VarPtr(v), lenth Dim ptr As Long CopyMemory VarPtr(ptr), VarPtr(b(8)) , 4 Dim Value As Byte CopyMemory VarPtr(Value), ptr, 2 Printf "VarType(v) = 40 00 00 00 00 00 00 50 ef 19 00 00 00 00 00, ptr = 0x19ef50, Value = 10 其他不多演示,注意Dim Value As语句下面的CopyMemory
使用 CopyMemory 拷贝内存 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ++ [Benchmark(Description = "CopyMemory")] ++ [Arguments(3840, 2160)] ++ [Arguments(100, 100)] public unsafe void CopyMemory(int width, int height)); _bitmap.Unlock(); } 1 2 [DllImport("kernel32.dll")] private static extern void CopyMemory ); _bitmap.Unlock(); } 基准测试数据 我们跑一次基准测试: Method Mean Error StdDev Median Ratio RatioSD CopyMemory 0.0776 ms 0.2250 ms 3.200 ms 1.00 0.00 ‘for for’ 10.401 ms 0.1979 ms 0.4964 ms 10.396 ms 3.21 0.25 ‘CopyMemory
gdiplus" (ByVal numEncoders As Long, ByVal Size As Long, Encoders As Any) As Long Private Declare Sub CopyMemory ' 设置参数的值:品质等级,最高为100,图像文件大小与品质成正比 End With CopyMemory Buffer(1 To Size) As Byte GdipGetImageEncoders Num, Size, Buffer(1) '得到数组和字符数据 CopyMemory PtrToStrW(Info(I).MimeType), strMimeType, vbTextCompare) = 0) Then '必须把指针转换成可用的字符 CopyMemory (lpsz) If Length > 0 Then Out = StrConv(String$(Length, vbNullChar), vbUnicode) CopyMemory
Sub TestObject() Dim rng As Range Set rng = Range("A1") Dim VarPtr中保存的数据 As Long CopyMemory TestObjectByValByRef rng End Sub Function TestObjectByVal(ByVal rng As Range) Dim VarPtr中保存的数据 As Long CopyMemory 中保存的数据 End Function Function TestObjectByValByRef(ByRef rng As Range) Dim VarPtr中保存的数据 As Long CopyMemory
if(dwSize >MEM_BLOCK_SIZE) { cout<<"over flow堆栈溢出"; return FALSE; } CopyMemory <<endl; FillMemory(lpSrc,MEM_BLOCK_SIZE,0xBB); FillMemory(lpSrc,MEM_BLOCK_SIZE/2,0xAA); CopyMemory
this.addressLimit - numBytes && otherPointer <= target.addressLimit - numBytes) { UNSAFE.copyMemory otherPos <= seg2.addressLimit - len) { // this -> temp buffer UNSAFE.copyMemory tempBuffer, BYTE_ARRAY_BASE_OFFSET, len); // other -> this UNSAFE.copyMemory length) { final long arrayAddress = BYTE_ARRAY_BASE_OFFSET + offset; UNSAFE.copyMemory length) { final long arrayAddress = BYTE_ARRAY_BASE_OFFSET + offset; UNSAFE.copyMemory
H2 Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT) #If Win64 Then Private Declare PtrSafe Sub CopyMemory IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long #Else Private Declare Sub CopyMemory GlobalLock(hData) If lSize > 0 Then ReDim abData(0 To CLng(lSize) - CLng(1)) As Byte CopyMemory
allocateMemory:分配内存空间,并返回偏移量 public native long allocateMemory(long bytes); copyMemory:复制内存空间 // @since 1.7 public native void copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset , long bytes); public void copyMemory(long srcAddress, long destAddress, long bytes) { copyMemory
'10 官方定义str长度 '4 变量占用 '2 字符长度 '2 字符后面00 00 Dim b(10 - 4 + 2 + 2 - 1) As Byte CopyMemory = 0x%x", hdll Dim str As String Dim lStrPtr As Long lStrPtr = RetStrPtr() + 6 CopyMemory Printf "强制赋值VarPtr前,StrPtr(str) = 0x%x", StrPtr(str) '+6 StrPtr指向的字符开始的位置,不包含前面00 88和长度信息4个 CopyMemory
第2处的代码创建了一个对象,这一行代码可以分解成3个操作: Copymemory = allocate(); // 1:分配对象的内存空间 ctorInstance(memory); // 2:初始化对象 例如: Copymemory = allocate(); // 1:分配对象的内存空间 instance = memory; // 3:设置instance指向刚分配的内存地址 // 注意,此时对象还没有被初始化
Quality = 100 23 End If 24 .Value = VarPtr(Quality) 25 End With 26 Call CopyMemory GlobalSize(Memory) 32 OldMemory = GlobalLock(Memory) 33 ReDim Data(0 To MemorySize - 1) 34 CopyMemory
ConvertThreadToFiber() 使用 PAGE_EXECUTE_READWRITE 权限在当前进程中分配内存 VirtualAlloc() 将 shellcode 复制到分配的内存空间 CopyMemory MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE); for (int i=0;i<sizeof(data);i++) { data[i]^=99; } CopyMemory