473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StructLayoutAtt ribute 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(La youtKind.Explic it, Size=8, CharSet=CharSet .Ansi, Pack=1)]
public struct StructTest
{
[FieldOffset(0)]
[MarshalAs(Unman agedType.I8]
public long field;

[FieldOffset(0)]
[MarshalAs(Unman agedType.ByValA rray, SizeConst=8)]
public byte[] overlay;

public StrucTest ( long l ) ....
Nov 16 '05 #1
2 2967
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.o rg> wrote in message
news:%2******** *******@TK2MSFT NGP12.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
10668
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 have to type the password for my certificate every time my program signs a mail :o( My program is a windows service running on a server so typing...
0
1753
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 { /// <summary>
3
3071
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 code. then I can see the data and everything gets updated, until the next time I put a break in the code. Otherwise, I have to put a break on the...
1
1279
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 earlier in this newsgroup. So in trying some funky things with StructLayout and FieldOffset to get a solution to that problem, I wrote some code...
10
2059
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 parameter. What would I need to do in order to convert a struct that looks like this: typedef struct { char rsvd0; char iadl1;
23
6972
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 dialog but just need to change the button text from "open" to "delete". Any ideas? Thanks
9
12676
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
3288
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 object to access. For example in the search function below I'd like to pass in an offset to the member I'd would like to compare against (in this...
10
11315
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 structure in memory. This field indicates the packing size that should be used when the LayoutKind.Sequential value is specified. The value of Pack must...
2
1877
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 to get the DDEInitialize to start the callback. I was hoping a more seasoned .net programmer might be able to help translate the following snippet...
0
7701
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...
0
7615
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...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7677
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...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
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
0
940
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.