如何将非托管内存数组复制到同一个非托管内存中

Mix*_*xer 4 c# memory unmanaged copy

我保留了10个128字节的内存

IntPtr dst = Marshal.AllocHGlobal (10 * 128);

IntPtr src1 = Marshal.AllocHGlobal (128);
// .... init scr1 from DLL
IntPtr src2 = Marshal.AllocHGlobal (128);
// .... init scr2 from DLL
Run Code Online (Sandbox Code Playgroud)

我需要的128个字节元素复制src1src2dst指定的偏移量.

Marshal.Copy不适合此类用途.自从srcdst非托管内存区域.

Age*_*ire 5

Window的API函数memcopy应该可以解决问题.

[DllImport("msvcrt.dll", EntryPoint = "memcpy",
    CallingConvention = CallingConvention.Cdecl, 
    SetLastError = false)]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count);
Run Code Online (Sandbox Code Playgroud)

另外,看看这个:

/sf/answers/186087611/

正如它声称的那样,您可以使用unsafe上下文手动传输必要的字节.