473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

StructLayoutAttribute and C-style union

Jim
Hi all,

I am trying to create the union of a structure and a buffer. This was
used often in embedded assembly and C programs to transfer binary data
accross serial ports. Is there an equivalent in C#? Normally, just about
anything is possible, but I can't find a way around the type checking.

Please don't hold my feet to the fire on my C syntax :). It has been
years since I wrote in C. It should convey the idea.

C:
typedef struct S
{
int a;
int b;
int c;
}

typedef union U
{
S structure,
char[sizeof(S)] buffer;
}

C#:
[StructLayout(LayoutKind.Explicit, Size=8, CharSet=CharSet.Ansi, Pack=1)]
public struct StructTest
{
[FieldOffset(0)]
[MarshalAs(UnmanagedType.I8]
public long field;

[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public byte[] overlay;

public StrucTest ( long l ) ....
Nov 16 '05 #1
2 2956
Is there an equivalent in C#?


In C# v2 there's a new fixed array feature that will make this a bit
easier.

In v1, I think the easiest way is to use the BitConverter class (or
manual bit shifting) to convert between the long value and a byte
array.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Jim
Thanks, Mattias.

Jim
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Is there an equivalent in C#?


In C# v2 there's a new fixed array feature that will make this a bit
easier.

In v1, I think the easiest way is to use the BitConverter class (or
manual bit shifting) to convert between the long value and a byte
array.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kim | last post by:
I have a .NET service sending mails using CDOEX. These mails I need to sign. I got a tip that I should use CAPICOM. That worked fine sending a mail with signature. BUT the problem is that I...
0
by: Robert Colfin | last post by:
Can anyone verify if this is the correct way to do this? Thanks. using System; using System.Threading; using System.Diagnostics; using System.Runtime.InteropServices; namespace WSockImport...
3
by: Marco Martin | last post by:
Hi, I'm writing an application that reads a serial port and 1)updates a list and sends data to a graph. When I run the application, nothing happens unless I put a break point anywhere in my...
1
by: Richard A. Lowe | last post by:
I've seen a number of people mention potential problems with the StructLayoutAttribute possibly causing problems and it came up again in Ray Z's post titled "covert between byte and int" just a bit...
10
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required...
23
by: George | last post by:
Is there a way to customize the open file common dialog? I am trying to modify the button text so I can create a delete file common dialog. I need the same functionality of the open file common...
9
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
9
by: rellik | last post by:
Hi All, Is there a C# version of the C++ pointer (or reference) to member operator - ..* or ->* Basically I'd like to write a function that can take an object and an offset to a member of the...
10
by: giddy | last post by:
hi , I' know what the StructLayoutAttribute does. I've seen it being used. But what does the Pack field do!? This is what MSDN says: Controls the alignment of data fields of a class or...
2
by: RFleming | last post by:
Hello, I am trying to change a DDEML callback function I have used in VB6 successfully for years now to vb.net. However, I can't seem to get my mind around how to setup a delegate and pointers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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...

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.