Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

Passing a struct containing a union from c# to win32

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
johannblake@yahoo.com - 16 Jun 2005 13:37 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.