C# 如何从 IntPtr 获取 Byte[]

发布于 2024-10-29 06:52:21 字数 210 浏览 1 评论 0原文

我有一个具有委托的 .dll(不是我自己的)。这个委托回调函数是:
"CallBackFN(ushort opCOde, IntPtr Payload, uint size, uint localIP)"

如何将 IntPtr 转换为 Byte[]?我认为有效负载实际上是Byte[]。如果它不是 Byte[] 并且是其他东西,我会丢失一些数据吗?

I have a .dll(not my own) that has a delegate. This delegate Callback function is:
"CallBackFN(ushort opCOde, IntPtr payload, uint size, uint localIP)"

How can i convert IntPtr to Byte[]? I think that payload is actually Byte[]. If it's not Byte[] and it's something else would i lose some data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

待天淡蓝洁白时 2024-11-05 06:52:21

如果是byte[]数组:

 byte[] managedArray = new byte[size];
 Marshal.Copy(pnt, managedArray, 0, size);

如果不是byte[],则Marshal.Copy中的size参数是数组中元素的个数,而不是字节大小。因此,如果您有一个 int[] 数组而不是 byte[] 数组,则必须除以 4(每个 int 的字节数)才能获得正确的数量要复制的元素,假设通过回调传递的大小参数指的是字节数。

If it's a byte[] array:

 byte[] managedArray = new byte[size];
 Marshal.Copy(pnt, managedArray, 0, size);

If it's not byte[], the size parameter in of Marshal.Copy is the number of elements in the array, not the byte size. So, if you had an int[] array rather than a byte[] array, you would have to divide by 4 (bytes per int) to get the correct number of elements to copy, assuming your size parameter passed through the callback refers to the number of bytes.

梓梦 2024-11-05 06:52:21

如果需要性能,直接使用:

unsafe { 
    byte *ptr = (byte *)buffer.ToPointer();

    int offset = 0;
    for (int i=0; i<height; i++)
    {
        for (int j=0; j<width; j++)
        {

            float b = (float)ptr[offset+0] / 255.0f;
            float g = (float)ptr[offset+1] / 255.0f;
            float r = (float)ptr[offset+2] / 255.0f;
            float a = (float)ptr[offset+3] / 255.0f;
            offset += 4;

            UnityEngine.Color color = new UnityEngine.Color(r, g, b, a);
            texture.SetPixel(j, height-i, color);
        }
    }
}

If you need performance, use it directly:

unsafe { 
    byte *ptr = (byte *)buffer.ToPointer();

    int offset = 0;
    for (int i=0; i<height; i++)
    {
        for (int j=0; j<width; j++)
        {

            float b = (float)ptr[offset+0] / 255.0f;
            float g = (float)ptr[offset+1] / 255.0f;
            float r = (float)ptr[offset+2] / 255.0f;
            float a = (float)ptr[offset+3] / 255.0f;
            offset += 4;

            UnityEngine.Color color = new UnityEngine.Color(r, g, b, a);
            texture.SetPixel(j, height-i, color);
        }
    }
}
一刻暧昧 2024-11-05 06:52:21

根据这个Stack Overflow问题,您可以执行以下操作:

var byteArray = new byte[dataBlockSize];
System.Runtime.InteropServices.Marshal.Copy(payload, byteArray, 0, dataBlockSize);

According to this Stack Overflow question, you can do the following:

var byteArray = new byte[dataBlockSize];
System.Runtime.InteropServices.Marshal.Copy(payload, byteArray, 0, dataBlockSize);
梦旅人picnic 2024-11-05 06:52:21

Span 可能是一个更好的解决方案,因为它提供了字节数组所需的大多数功能。它更快,因为您不需要分配和复制到新的缓冲区,并且更安全,因为您不必直接使用指针。

IntPtr ptr = ... ; 
int ptrLength = ...; 


unsafe
{
    Span<byte> byteArray = new Span<byte>(ptr.ToPointer(), ptrLength);

    for (int i = 0; i < byteArray.Length; i++ )
    {
        // Use it as normalarray array ;
        byteArray[i] = 6;
    }

    // You can always get a byte array . Caution, it allocates a new buffer
    byte[] realByteArray = byteArray.ToArray();
}

它包含在 .NET Core 2.1 和用于 . NET Framework 4.5 + 和 .NET Core 2.0 +;

Span<byte> may be a better solution as it provides most features you need from a byte array. It is faster as you won't need to allocate and copy to a new buffer and safer as you will not have to use directly the pointer.

IntPtr ptr = ... ; 
int ptrLength = ...; 


unsafe
{
    Span<byte> byteArray = new Span<byte>(ptr.ToPointer(), ptrLength);

    for (int i = 0; i < byteArray.Length; i++ )
    {
        // Use it as normalarray array ;
        byteArray[i] = 6;
    }

    // You can always get a byte array . Caution, it allocates a new buffer
    byte[] realByteArray = byteArray.ToArray();
}

It's included in.NET Core 2.1 and a nuget package (System.Memory) for .NET Framework 4.5 + and .NET Core 2.0 +;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文