I need to assemble a blob in a Powershell script that shows the following layout:
_Pragma("pack(1)")
struct MyConfig {
uint16_t level;
uint16_t thresholds[16];
// ... the struct contains lots of POD members, but no pointers
}
struct MyConfig config = {
.level = 1,
.thresholds = {
1, 2, 3, 4, ...
}
};
The resulting config struct instance shall be dumped to a file.
I am able to solve the first part for integral types, but I am not able to access array members:
$typeCode = @'
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[Serializable]
public struct MyConfig
{
public Int16 level;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public Int32[] thresholds;
}
'@
Add-Type -TypeDefinition $typeCode
$config = New-Object MyConfig
$config.level = 1 # works
$config.thresholds[0] = 1 # Cannot index into a null array.
I have not looked at serializing the struct, yet.