I have a powershell script with an added type:
Add-Type -TypeDefinition @'
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[Serializable]
public struct md_size {
[MarshalAs(UnmanagedType.U4)] public uint md_type;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public byte[] md_data;
} ;
}
...'
I need to convert this to a byte array, to send it on the wire.
I have tried using the BinaryFormatter:
$in = ... (object of type md_size)
$mstr = New-Object System.IO.MemoryStream
$fmt = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$fmt .Serialize($mstr , $in)
$result = $mstr.GetBuffer()
And I expect to get an array of size of 260 back, but I get a size of 256, which I don't quite understand.
How can I convert my struct into a byte[] array?