469,656 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,656 developers. It's quick & easy.

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

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/247referenc.../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.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
4 13654
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" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com...
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/247referenc.../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.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2
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" wrote:
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" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com...
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/247referenc.../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.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #3
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" <ch************@nospam.hotmail.com> wrote in message
news:24**********************************@microsof t.com...
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" wrote:
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" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com...
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/247referenc.../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.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #4
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" wrote:
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" <ch************@nospam.hotmail.com> wrote in message
news:24**********************************@microsof t.com...
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" wrote:
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" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com...
> 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/247referenc.../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.
> --
> Regards,
> Chua Wen Ching :)


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by (Pete Cresswell) | last post: by
3 posts views Thread by Paradigm | last post: by
6 posts views Thread by Neil Zanella | last post: by
2 posts views Thread by Barry Schwarz | last post: by
4 posts views Thread by Girish | last post: by
18 posts views Thread by ranjeet.gupta | last post: by
10 posts views Thread by piboye | last post: by
30 posts views Thread by Yevgen Muntyan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.