读取C / C ++数据在C#中的结构,从一个字节数组 [英] Reading a C/C++ data structure in C# from a byte array

查看:121
本文介绍了读取C / C ++数据在C#中的结构,从一个字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是填补从byte []数组,其中的数据是从C / C ++结构一个C#结构的最佳方式是什么?在C结构会是这个样子(我的C是非常生疏):

What would be the best way to fill a C# struct from a byte[] array where the data was from a C/C++ struct? The C struct would look something like this (my C is very rusty):

typedef OldStuff {
    CHAR Name[8];
    UInt32 User;
    CHAR Location[8];
    UInt32 TimeStamp;
    UInt32 Sequence;
    CHAR Tracking[16];
    CHAR Filler[12];
}

和将填补这样的:

[StructLayout(LayoutKind.Explicit, Size = 56, Pack = 1)]
public struct NewStuff
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
    [FieldOffset(0)]
    public string Name;

    [MarshalAs(UnmanagedType.U4)]
    [FieldOffset(8)]
    public uint User;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
    [FieldOffset(12)]
    public string Location;

    [MarshalAs(UnmanagedType.U4)]
    [FieldOffset(20)]
    public uint TimeStamp;

    [MarshalAs(UnmanagedType.U4)]
    [FieldOffset(24)]
    public uint Sequence;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
    [FieldOffset(28)]
    public string Tracking;
}

什么是复制 OldStuff Newstuff文件,如果 OldStuff 获通过为byte []数组?

What is best way to copy OldStuff to NewStuff, if OldStuff was passed as byte[] array?

目前,我正在做一些像下面,但感觉那种沉闷。

I'm currently doing something like the following, but it feels kind of clunky.

GCHandle handle;
NewStuff MyStuff;

int BufferSize = Marshal.SizeOf(typeof(NewStuff));
byte[] buff = new byte[BufferSize];

Array.Copy(SomeByteArray, 0, buff, 0, BufferSize);

handle = GCHandle.Alloc(buff, GCHandleType.Pinned);

MyStuff = (NewStuff)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
    typeof(NewStuff));

handle.Free();

有没有更好的方式来做到这一点?

Is there better way to accomplish this?

会使用 BinaryReader在类提供了寄托记忆和使用任何性能提升 Marshal.PtrStructure

Would using the BinaryReader class offer any performance gains over pinning the memory and using Marshal.PtrStructure?

推荐答案

从我可以在这方面看,你不需要复制SomeByteArray到缓冲区中。您只需从SomeByteArray得到处理,别起来,用PtrToStructure复制IntPtr的数据,然后松开。不需要副本。

From what I can see in that context, you don't need to copy SomeByteArray into a buffer. You simply need to get the handle from SomeByteArray, pin it, copy the IntPtr data using PtrToStructure and then release. No need for a copy.

这将是:

NewStuff ByteArrayToNewStuff(byte[] bytes)
{
    GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
    NewStuff stuff = (NewStuff)Marshal.PtrToStructure(
        handle.AddrOfPinnedObject(), typeof(NewStuff));
    handle.Free();
    return stuff;
}

通用版:

T ByteArrayToStructure<T>(byte[] bytes) where T: struct 
{
    GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
    T stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
        typeof(T));
    handle.Free();
    return stuff;
}

...

这篇关于读取C / C ++数据在C#中的结构,从一个字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆