I need to marshall a struct containing a union. The union part is not
being correctly passed. Any ideas on what I am missing or doing wrong?
Thanks for your help!
Johann
The win32 struct looks like this:
typedef struct {
WAVEFORMATEX Format;
union {
WORD wValidBitsPerSample;
WORD wSamplesPerBlock;
WORD wReserved;
} Samples;
DWORD dwChannelMask;
GUID SubFormat;
} WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
This is how I converted the struct to c#...
[StructLayout(LayoutKind.Sequential)] public struct WaveFormatEx
{
public ushort wFormatTag;
public ushort nChannels;
public uint nSamplesPerSec;
public uint nAvgBytesPerSec;
public ushort nBlockAlign;
public ushort wBitsPerSample;
public ushort cbSize;
}
[StructLayout(LayoutKind.Explicit)] public struct Samples
{
[FieldOffset(0)] public ushort wValidBitsPerSample;
[FieldOffset(0)] public ushort wSamplesPerBlock;
[FieldOffset(0)] public ushort wReserved;
}
[StructLayout(LayoutKind.Sequential)] public struct
WaveFormatExtensible
{
public WaveFormatEx waveFormatEx;
public Samples samples;
public uint dwChannelMask;
public Guid SubFormat;
}
WaveFormatExtensible fmt = new WaveFormatExtensible();
WaveFormatEx waveFormatEx = new WaveFormatEx();
waveFormatEx.wFormatTag = 0xFFFE;
waveFormatEx.nChannels = 2;
waveFormatEx.nSamplesPerSec = 44100;
waveFormatEx.nAvgBytesPerSec = 44100 * 4 * 2;
waveFormatEx.nBlockAlign = 8;
waveFormatEx.wBitsPerSample = 24;
waveFormatEx.cbSize = (ushort)
Marshal.SizeOf(typeof(WaveFormatExtensible));
fmt.waveFormatEx = waveFormatEx;
WaveNative.Samples samples = new WaveNative.Samples();
samples.wValidBitsPerSample = 24; // These 3 members end up not being
passed correctly.
samples.wSamplesPerBlock = 24;
samples.wReserved = 24;
fmt.samples = samples;
fmt.dwChannelMask = 3;
fmt.SubFormat = new Guid("00000001-0000-0010-8000-00aa00389b71");
Willy Denoyette [MVP] - 16 Jun 2005 20:03 GMT
Check the C struct alignment requirements, probably the Samples struct is
not correctly aligned.
Willy.
>I need to marshall a struct containing a union. The union part is not
> being correctly passed. Any ideas on what I am missing or doing wrong?
[quoted text clipped - 71 lines]
>
> fmt.SubFormat = new Guid("00000001-0000-0010-8000-00aa00389b71");
prakash - 17 Jun 2005 15:31 GMT
Try this declarations Johann
--------------------------------------------------------------------------------------------------
[StructLayout(LayoutKind.Sequential)]
public struct WaveFormatEx
{
[MarshalAs(UnmanagedType.U2)]
public ushort wFormatTag;
[MarshalAs(UnmanagedType.U2)]
public ushort nChannels;
[MarshalAs(UnmanagedType.U4)]
public uint nSamplesPerSec;
[MarshalAs(UnmanagedType.U4)]
public uint nAvgBytesPerSec;
[MarshalAs(UnmanagedType.U2)]
public ushort nBlockAlign;
[MarshalAs(UnmanagedType.U2)]
public ushort wBitsPerSample;
[MarshalAs(UnmanagedType.U2)]
public ushort cbSize;
}
[StructLayout(LayoutKind.Explicit)]
public struct Samples
{
[FieldOffset(0),MarshalAs(UnmanagedType.U2)] public ushort
wValidBitsPerSample;
[FieldOffset(0),MarshalAs(UnmanagedType.U2)] public ushort
wSamplesPerBlock;
[FieldOffset(0),MarshalAs(UnmanagedType.U2)] public ushort wReserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct WaveFormatExtensible
{
public WaveFormatEx waveFormatEx;
public Samples samples;
[MarshalAs(UnmanagedType.U4)]
public uint dwChannelMask;
public Guid SubFormat;
}
--------------------------------------------------------------------------------------------------
Regards
Prakash
Willy Denoyette [MVP] - 18 Jun 2005 11:26 GMT
In what way is this different, other than it clutters the namespace with
unnecessary attributes?
Johann's problem is alignment and packing related.
Willy.
> Try this declarations Johann
> --------------------------------------------------------------------------------------------------
[quoted text clipped - 45 lines]
> Regards
> Prakash