C# 将数据从 IntPtr 复制到 IntPtr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15975972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:39:04  来源:igfitidea点击:

Copy data from from IntPtr to IntPtr

c#

提问by Doc Brown

I have two IntPtrvalues pointing to some data areas of lengthbytes. lengthmay have an order of magnitude of 200k to 400k.

我有两个IntPtr值指向一些length字节数据区域。length可能有 200k 到 400k 的数量级。

            int length = /* ..*/
            IntPtr ptrSrc = /*.. */;
            IntPtr ptrDst = /* .. */;

Now I want to copy the data from ptrSrcto ptrDst. This code works fine:

现在我想将数据从 复制ptrSrcptrDst. 这段代码工作正常:

            byte[] data = new byte[length];
            Marshal.Copy(ptrSrc, data, 0, length);
            Marshal.Copy(data, 0, ptrDst, length);

but it has the drawback of needing an additional temporary (potentially huge) array. Unfortunately, I could not find a Marshal.Copyvariant in the .NET framework for copying directly from IntPtrto IntPtr, so I am looking for alternatives.

但它的缺点是需要额外的临时(可能很大)数组。不幸的是,我Marshal.Copy在 .NET 框架中找不到直接从IntPtrto复制的变体IntPtr,所以我正在寻找替代方案。

I am interested in a solution which works on 32 Windows as well as 64 bit Windows. Any suggestions?

我对适用于 32 位 Windows 和 64 位 Windows 的解决方案感兴趣。有什么建议?

采纳答案by driis

You can P/Invoke into the appropiate C function. That is probably the easiest way of doing that. Example:

您可以 P/Invoke 进入适当的 C 函数。这可能是最简单的方法。例子:

class Program
{
    [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
    public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

    static void Main()
    {
        const int size = 200;
        IntPtr memorySource = Marshal.AllocHGlobal(size);
        IntPtr memoryTarget = Marshal.AllocHGlobal(size);

        CopyMemory(memoryTarget,memorySource,size);
    }
}

回答by user3800527

I think the answer needs an update in .net 4.6 there is

我认为答案需要在 .net 4.6 中有更新

 Buffer.MemoryCopy Method (Void*,?Void*,?Int64,?Int64)

This method copies sourceBytesToCopy bytes from the address specified by source to the address specified by destination. If the buffers overlap and the difference between destination minus source is less than sourceBytesToCopy, the source block is copied to the destination block in reverse order.

此方法将 sourceBytesToCopy 字节从 source 指定的地址复制到 destination 指定的地址。如果缓冲区重叠并且目标与源之间的差值小于 sourceBytesToCopy,则源块以相反的顺序复制到目标块。

So if you not on 4.6 or universal windows app 10 then use the previous answer.

因此,如果您不在 4.6 或通用 Windows 应用程序 10 上,请使用上一个答案。

回答by cdiggins

As user38000527 points outthe modern answer is MemoryCopyand it is part of .NET core 1.0, .NET standard 1.3, and .NET framework 4.6.

正如user38000527 指出的那样,现代答案是MemoryCopy,它是 .NET core 1.0、.NET standard 1.3 和 .NET framework 4.6 的一部分。

Here is how you use it in your context:

以下是您在上下文中使用它的方式:

Buffer.MemoryCopy(ptrSrc.ToPointer(), ptrDest.ToPointer(), length, length)