473,804 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Union -> C# struct LayoutKind.Expl icit 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.Expl ict 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 13909
Try this

[StructLayout(La youtKind.Explic it)]
public struct Message
{
[StructLayout(La youtKind.Explic it)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(La youtKind.Explic it)]
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******** *************** ***********@mic rosoft.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.Expl ict 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(La youtKind.Explic it)]
public struct Message
{
[StructLayout(La youtKind.Explic it)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(La youtKind.Explic it)]
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******** *************** ***********@mic rosoft.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.Expl ict 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******** *************** ***********@mic rosoft.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(La youtKind.Explic it)]
public struct Message
{
[StructLayout(La youtKind.Explic it)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(La youtKind.Explic it)]
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******** *************** ***********@mic rosoft.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.Expl ict 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******** *************** ***********@mic rosoft.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(La youtKind.Explic it)]
public struct Message
{
[StructLayout(La youtKind.Explic it)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(La youtKind.Explic it)]
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******** *************** ***********@mic rosoft.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.Expl ict 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
5511
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
3577
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 from letdata UNION SELECT as ID FROM MEMODATA; I get an ODBC error. The same query runs when the backend files are MDB files and it runs with MYSQL if I only combine 2 tables.
6
3335
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 gcc 3.2.2 but does not produce the expected results, the expected results being the ones annotated in the comments in the code): #include <stdlib.h>
2
4360
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 undefined behavior because my reference to the union is via a
10
5079
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
2941
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
2383
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 compiler), and it is, what I conclude from the below code union data_type {
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 destructor ,or other member functions. I can understand using constructors and memeber functions,but what is destructor used for? I have appealled to the forums in chinese ,but no enough usefull feedback.
30
3291
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
3850
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 table, we try separate this big table into twelve tables and create a view
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.