如题,有两种方法,但是侧重点不同,如果是指针拷贝到指针,用如下的方法:
1、RtlCopyMemory与RtlMoveMemory
这两个函数是内核函数api,对应Win32 API是CopyMemory和MoveMemory。都能实现内存块的复制,两者的区别在于CopyMemory是非重叠内存区域的复制,MoveMemory可以不考虑是否重叠,都可以安全复制。
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi)]
public extern static long CopyMemory(IntPtr dest, IntPtr source, int size);
//使用
CopyMemory(destPtr,srcPtr,size);
2、Marshal类
Marshal.copy()方法用来在托管对象(数组)和非托管对象(IntPtr)之间进行内容的复制。但是它只能实现数组拷贝到指针对象或指针对象拷贝到数组里面。如下:
Marshal.Copy(float[] source,int startIndex,IntPtr dest,int length);
Marshal.Copy(IntPtr source,float[] dest,int startIndex,int length);
也基本够用了,写在这里备份一下。