Add marshaler for little endian structures.

This commit is contained in:
2018-02-27 20:22:56 +00:00
parent 396f982600
commit 6419fcf8ed

View File

@@ -48,6 +48,20 @@ namespace libexeinfo
return SwapStructureMembersEndian(str); return SwapStructureMembersEndian(str);
} }
/// <summary>
/// Marshals a little endian structure from a byte array.
/// </summary>
/// <returns>The structure.</returns>
/// <param name="bytes">Byte array.</param>
/// <typeparam name="T">Structure type.</typeparam>
public static T ByteArrayToStructureLittleEndian<T>(byte[] bytes) where T : struct
{
GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned);
T str = (T)Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T));
ptr.Free();
return str;
}
/// <summary> /// <summary>
/// Swaps endian of structure members that correspond to numerical types. /// Swaps endian of structure members that correspond to numerical types.
/// Does not traverse nested structures. /// Does not traverse nested structures.