I am trying to align a C# class with C++ class so I can RPM the structure in C#. The C++ class contains a pad of 0x10 but I can't seem to align my C# class with my C++ class.
C++ Class:
class Value
{
public:
char pad_0x0000[0x10]; //0x0000
float current; //0x0010
float maximum; //0x0014
};
My C# Class:
class Value
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)]
public byte[] pad_0x0000 = new byte[0x10]; //0x0000
public float current; //0x0010
public float maximum; //0x0014
};
The issue is that whenever I attempt to get the size of the structure using Marshal.SizeOf I get this error:
Value cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
I may be going at this all wrong. All I'm trying to do is pad by 0x10 so the 2 classes are aligned
Thanks in advance
EDIT: I followed Hans Passant's advice and now it compiles and runs correctly
[StructLayout(LayoutKind.Sequential)]
class Value
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)]
public byte[] pad_0x0000 = new byte[0x10]; //0x0000
public float current; //0x0010
public float maximum; //0x0014
};
but, the value of current is still incorrect which means that they aren't aligned correctly. So my question now is: Isn't this
char pad_0x0000[0x10]; //0x0000
the same size as this
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)]
public byte[] pad_0x0000 = new byte[0x10]; //0x0000
And if not, then how do I get the equivalent of the C++ pad in c#?