C Union -> C# struct LayoutKind.Explicit FieldOffset - how?

G

Guest

Hi there,

I had a union in C:

typedef union TagMessage
{
struct {
unsigned char avalue;
unsigned char bvalue;
} a;
struct {
unsigned short cvalue;
} b;
} Message;

From these few links, there is a possible way is to declare a struct with
LayoutKind.Explict and use FieldOffset.

http://www.dotnet247.com/247reference/msgs/18/93017.aspx
http://www.hanselman.com/blog/PermaLink.aspx?guid=187

The problem is how do i relate the FieldOffset in my scenarios. I had 2
structs in my C union. How do i achieve it with the help of that?

Any idea please? Thanks.
 
S

Sijin Joseph

Try this

[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}

[FieldOffset(0)]public BStruct b;
}
 
G

Guest

Thanks Sijin, i had 1 question.

How do i know what argument to be past in here:

FieldOffset(0) or (1) or (16)

Hmm, can you give me some examples? I can't find proper explanation even on
msdn.

Thanks a lot. :) Really appreciate it.

Sijin Joseph said:
Try this

[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}

[FieldOffset(0)]public BStruct b;
}
 
S

Sijin Joseph

The FieldOffset attribute defines the number of bytes from the start of the
structure where this field will be layed out in memory.

In your case avalue and b value are unsigned chars which take 1 byte so they
will be layed out at mem locations 0 and 1. cvalue is a unsigned short since
it is a union that will also be layed out at offset 0 from the start of the
structure and take 2 bytes.

Basically FieldOffset defines the number of bytes from the start of the
structure where the field will be placed.


Let me know if you need more hlp.
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Chua Wen Ching said:
Thanks Sijin, i had 1 question.

How do i know what argument to be past in here:

FieldOffset(0) or (1) or (16)

Hmm, can you give me some examples? I can't find proper explanation even on
msdn.

Thanks a lot. :) Really appreciate it.

Sijin Joseph said:
Try this

[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}

[FieldOffset(0)]public BStruct b;
}

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Chua Wen Ching said:
Hi there,

I had a union in C:

typedef union TagMessage
{
struct {
unsigned char avalue;
unsigned char bvalue;
} a;
struct {
unsigned short cvalue;
} b;
} Message;

From these few links, there is a possible way is to declare a struct with
LayoutKind.Explict and use FieldOffset.

http://www.dotnet247.com/247reference/msgs/18/93017.aspx
http://www.hanselman.com/blog/PermaLink.aspx?guid=187

The problem is how do i relate the FieldOffset in my scenarios. I had 2
structs in my C union. How do i achieve it with the help of that?

Any idea please? Thanks.
 
G

Guest

Hi Sijin,

Thanks a lot for the explanation. Ok, i will give it a try when i get back
to office, i hope it will work.

Thanks again.

Sijin Joseph said:
The FieldOffset attribute defines the number of bytes from the start of the
structure where this field will be layed out in memory.

In your case avalue and b value are unsigned chars which take 1 byte so they
will be layed out at mem locations 0 and 1. cvalue is a unsigned short since
it is a union that will also be layed out at offset 0 from the start of the
structure and take 2 bytes.

Basically FieldOffset defines the number of bytes from the start of the
structure where the field will be placed.


Let me know if you need more hlp.
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Chua Wen Ching said:
Thanks Sijin, i had 1 question.

How do i know what argument to be past in here:

FieldOffset(0) or (1) or (16)

Hmm, can you give me some examples? I can't find proper explanation even on
msdn.

Thanks a lot. :) Really appreciate it.

Sijin Joseph said:
Try this

[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}

[FieldOffset(0)]public BStruct b;
}

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Hi there,

I had a union in C:

typedef union TagMessage
{
struct {
unsigned char avalue;
unsigned char bvalue;
} a;
struct {
unsigned short cvalue;
} b;
} Message;

From these few links, there is a possible way is to declare a struct with
LayoutKind.Explict and use FieldOffset.

http://www.dotnet247.com/247reference/msgs/18/93017.aspx
http://www.hanselman.com/blog/PermaLink.aspx?guid=187

The problem is how do i relate the FieldOffset in my scenarios. I had 2
structs in my C union. How do i achieve it with the help of that?

Any idea please? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top