473,499 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13888
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5447
by: (Pete Cresswell) | last post by:
I've got some SQL that works as far as returning a recordset from a series of UNION statements. viz: SELECT whatever UNION this UNION that UNION other
3
3540
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
6
3313
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
4337
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
5046
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
4
2925
by: Girish | last post by:
I have 2 differesnt defination of same Union as below and a piece of code for printing size of Union and its members.. union U { union U { int i; int j; }a;
18
2359
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
10
459
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and...
30
3223
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
3814
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.