I have a bunch of objects in powershell that I need to serialize into a data blob for using it in a C application. For reasons I cannot simply generate C source code and invoke the compiler. I am also ensuring that the serialized data and the layout expected by the C application will be compatible. I am not ever going to deserialize the data in Powershell.
The object:
$typeCode = @'
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[Serializable]
public struct MyConfig
{
public Byte Value1;
public UInt16 Value2;
}
'@
Add-Type -TypeDefinition $typeCode;
$config = [MyConfig]@{
Value1 = 1;
Value2 = 2;
};
I tried to serialize it using the BinaryFormatter:
$fs = New-Object System.IO.FileStream 'config.bin', ([System.IO.FileMode]::Create)
$bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$bf.Serialize($fs, $config)
$fs.Close()
But the resulting file contains all kind of meta information. I just want the raw data. For above example that would be a file of 3 bytes.
How can I achieve that?