By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Interop: union in a struct

P: n/a
Hello I have struct and there is a union in. (C DLL). Could someone
give me ideas how to do it? I just tried for a while but without
success...

thats the c code:
typedef struct {
unsigned int busType;
union {
struct {
unsigned int bitRate;
unsigned char sjw;
unsigned char tseg1;
unsigned char tseg2;
unsigned char sam; // 1 or 3
unsigned char outputMode;
}can;
unsigned char raw[32];
}data;
} XLbusParams;

thats my definition in VB.net

<StructLayout(LayoutKind.Sequential)> _
Structure s_xl_busParams
Public busType As UInt32
Public data As s_xl_UnionBusParams
End Structure

<StructLayout(LayoutKind.Explicit)> _
Structure s_xl_UnionBusParams
<FieldOffset(0)> Public can As s_xl_can
<FieldOffset(0)> <MarshalAs(UnmanagedType.ByValArray,
SizeConst:=32)> _
Public reserved() As UInt32
End Structure

but have a problem using the last field offset command. i tried it
also with
<FieldOffset(0)> raw() as byte

but havent success. in doc is an example with overload at declaration
but dont have an idea how to do it in a struct.
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Check out the structs tutorial section, "Attributes on Structs". Using that
technique, you can create a union.

"Markus Stiller" <fu****@gmx.net> wrote in message
news:68*************************@posting.google.co m...
Hello I have struct and there is a union in. (C DLL). Could someone
give me ideas how to do it? I just tried for a while but without
success...

thats the c code:
typedef struct {
unsigned int busType;
union {
struct {
unsigned int bitRate;
unsigned char sjw;
unsigned char tseg1;
unsigned char tseg2;
unsigned char sam; // 1 or 3
unsigned char outputMode;
}can;
unsigned char raw[32];
}data;
} XLbusParams;

thats my definition in VB.net

<StructLayout(LayoutKind.Sequential)> _
Structure s_xl_busParams
Public busType As UInt32
Public data As s_xl_UnionBusParams
End Structure

<StructLayout(LayoutKind.Explicit)> _
Structure s_xl_UnionBusParams
<FieldOffset(0)> Public can As s_xl_can
<FieldOffset(0)> <MarshalAs(UnmanagedType.ByValArray,
SizeConst:=32)> _
Public reserved() As UInt32
End Structure

but have a problem using the last field offset command. i tried it
also with
<FieldOffset(0)> raw() as byte

but havent success. in doc is an example with overload at declaration
but dont have an idea how to do it in a struct.

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.