从字节数组中读取 C# 中的 C/C++ 数据结构 [英] Reading a C/C++ data structure in C# from a byte array
问题描述
从数据来自 C/C++ 结构的 byte[] 数组填充 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];
}
并且会填充这样的内容:
And would fill something like this:
[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
的最佳方法是什么?
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);
try
{
NewStuff stuff = (NewStuff)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(NewStuff));
}
finally
{
handle.Free();
}
return stuff;
}
通用版本:
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;
}
更简单的版本(需要 unsafe
开关):
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));
}
}
这篇关于从字节数组中读取 C# 中的 C/C++ 数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!