从字节数组读取 C # 中的 C/C + + 数据结构

如果数据来自 C/C + + 结构,那么从 byte []数组中填充 C # 结构的最佳方法是什么?C 结构看起来像这样(我的 C 非常生疏) :

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作为 byte []数组传递,那么将 OldStuff复制到 NewStuff的最佳方法是什么?

我目前正在做下面这样的事情,但是感觉有点笨重。

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();

还有更好的办法吗?


使用 BinaryReader类会比固定内存和使用 Marshal.PtrStructure提供任何性能提升吗?

61875 次浏览

If you have a byte[] you should be able to use the BinaryReader class and set values on NewStuff using the available ReadX methods.

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.

That would be:

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

Generic version:

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

Simpler version (requires unsafe switch):

unsafe T ByteArrayToStructure<T>(byte[] bytes) where T : struct
{
fixed (byte* ptr = &bytes[0])
{
return (T)Marshal.PtrToStructure((IntPtr)ptr, typeof(T));
}
}

Watch out for packing issues. In the example you gave all fields are at the obvious offsets because everything is on 4 byte boundaries but this will not always be the case. Visual C++ packs on 8 byte boundaries by default.

object ByteArrayToStructure(byte[] bytearray, object structureObj, int position)
{
int length = Marshal.SizeOf(structureObj);
IntPtr ptr = Marshal.AllocHGlobal(length);
Marshal.Copy(bytearray, 0, ptr, length);
structureObj = Marshal.PtrToStructure(Marshal.UnsafeAddrOfPinnedArrayElement(bytearray, position), structureObj.GetType());
Marshal.FreeHGlobal(ptr);
return structureObj;
}

Have this

Here is an exception safe version of the accepted answer:

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