Take this example of a C# struct:
[StructLayout(LayoutKind.Explicit)]
public struct Example
{
[FieldOffset(0x10)]
public IntPtr examplePtr;
[FieldOffset(0x18)]
public IntPtr examplePtr2;
[FieldOffset(0x54)]
public int exampleInt;
}
I can take an array of bytes, and transform it to this struct like so:
public static T GetStructure<T>(byte[] bytes)
{
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
var structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
handle.Free();
return structure;
}
public static T GetStructure<T>(byte[] bytes, int index)
{
var size = Marshal.SizeOf(typeof(T));
var tmp = new byte[size];
Array.Copy(bytes, index, tmp, 0, size);
return GetStructure<T>(tmp);
}
GetStructure<Example>(arrayOfBytes);
Is there equivalent functionality in C++ to take an array of bytes and transform it to a struct, where not all bytes are used in the transformation (C# structlayout.explicit w/ field offsets)?
I don't want to do something like the following:
struct {
pad_bytes[0x10];
DWORD64 = examplePtr;
DWORD64 = examplePtr2;
pad_bytes2[0x44];
int exampleInt;
}