473,480 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Mashalling strange structs

Hi,

How would I marshall this struct in C#? Thanks in advance:

typedef struct
{
ULONG bConnected :1;
ULONG bPnPEnabled :1;
ULONG bEnabled :1;
ULONG :1;
ULONG ConnectionState :6;
} PNP_STATUS;

Jun 27 '08 #1
3 1176
wt****@gmail.com wrote:
How would I marshall this struct in C#? Thanks in advance:

typedef struct
{
ULONG bConnected :1;
ULONG bPnPEnabled :1;
ULONG bEnabled :1;
ULONG :1;
ULONG ConnectionState :6;
} PNP_STATUS;
Those are bitfields, for which C# has no native support. You can emulate
them with properties, though:

[StructLayout(LayoutKind.Sequential, Size = 4)]
struct PNP_STATUS {
private ushort s;

private bool getBit(int bit) {
return (s & (1 << bit)) != 0;
}

private void setBit(int bit, bool value) {
if (value) {
s |= (ushort) (1 << bit);
} else {
s &= (ushort) ~(1 << bit);
}
}

private int getIntSection(int startBit, int count) {
return (s >startBit) & ((1 << count) - 1);
}

private void setIntSection(int startBit, int count, int value) {
s |= (ushort) ((value & (1 << count) - 1) << startBit);
}

public bool bConnected {
get { return getBit(0); }
set { setBit(0, value); }
}

public bool bPnPEnabled {
get { return getBit(1); }
set { setBit(1, value); }
}

public bool bEnabled {
get { return getBit(2); }
set { setBit(2, value); }
}

public int ConnectionState {
get { return getIntSection(4, 6); }
set { setIntSection(4, 6, value); }
}
}

The "Size = 4" above forces aligning to a 4-byte boundary, which is probably
what the unmanaged side expects/wants too. You may need to change this, however.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
On Jun 11, 2:10 pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
wtf...@gmail.com wrote:
How would I marshall this struct in C#? Thanks in advance:
typedef struct
{
ULONG bConnected :1;
ULONG bPnPEnabled :1;
ULONG bEnabled :1;
ULONG :1;
ULONG ConnectionState :6;
} PNP_STATUS;

Those are bitfields, for which C# has no native support. You can emulate
them with properties, though:

[StructLayout(LayoutKind.Sequential, Size = 4)]
struct PNP_STATUS {
private ushort s;

private bool getBit(int bit) {
return (s & (1 << bit)) != 0;
}

private void setBit(int bit, bool value) {
if (value) {
s |= (ushort) (1 << bit);
} else {
s &= (ushort) ~(1 << bit);
}
}

private int getIntSection(int startBit, int count) {
return (s >startBit) & ((1 << count) - 1);
}

private void setIntSection(int startBit, int count, int value) {
s |= (ushort) ((value & (1 << count) - 1) << startBit);
}

public bool bConnected {
get { return getBit(0); }
set { setBit(0, value); }
}

public bool bPnPEnabled {
get { return getBit(1); }
set { setBit(1, value); }
}

public bool bEnabled {
get { return getBit(2); }
set { setBit(2, value); }
}

public int ConnectionState {
get { return getIntSection(4, 6); }
set { setIntSection(4, 6, value); }
}
}

The "Size = 4" above forces aligning to a 4-byte boundary, which is probably
what the unmanaged side expects/wants too. You may need to change this, however.

--
J.http://symbolsprose.blogspot.com
Thanks J, I didnt know what that struct was called or doing. This
cleared up a lot of questions.
Jun 27 '08 #3
On Jun 11, 8:10 pm, wtf...@gmail.com wrote:
Hi,

How would I marshall this struct in C#? Thanks in advance:

typedef struct
{
ULONG bConnected :1;
ULONG bPnPEnabled :1;
ULONG bEnabled :1;
ULONG :1;
ULONG ConnectionState :6;

} PNP_STATUS;
You might also want to have a look at BitVector32 struct. It
encapsulates a 32-bit int (as the name implies), and allows you to
"slice" it into chunks of several bits using the CreateSection method,
and then access them individually. For your case, it would be
something like this:

struct PNP_STATUS
{
public static readonly BitVector32.Section
bConnected = BitVector32.CreateSection(1), // 1 bit at the
beginning
bPnPEnabled = BitVector32.CreateSection(1, bConnected), // 1 bit
following bConnected
bEnabled = BitVector32.CreateSection(1, bPnPEnabled), // 1 bit
following bPnPEnabled
padding = BitVector32.CreateSection(1, bEnabled ), // 1 bit
following bEnabled
ConnectionState = BitVector32.CreateSection(1, padding); // 6 bits
following padding

public BitVector32 Data;
}

Then you can access it like this:

PNP_STATUS status;
if (status[PNP_STATUS.bConnected] == 1)
{
status[PNP_STATUS.ConnectionState]++;
}

When you have do deal with a large number of bitfields, this makes it
a little bit more obvious. Of course, it is still better to hide all
those Section objects inside the struct as private fields, and expose
them via properties.
Jun 27 '08 #4

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

Similar topics

3
2046
by: Sameh Halabi | last post by:
Hi I have a strange problem in C , I hope someone may help with it . the problem is as follows : I have an existing big structure (that I can't change) which contains in it groups of...
6
3235
by: Tom | last post by:
I try to write a simple tcp based job queue server. It's purpose is to get commands from a client and store them in a queue (STL). The server has a thread which checks periodically the queue,...
5
3107
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
4
1417
by: William Sullivan | last post by:
I have an extremely weird problem that I have no idea how to approach. I have a simple page with a search textbox and a search button. The button causes a postback, where I perform the search and...
5
9776
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
61
3683
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
2
1393
by: openbysource | last post by:
I wrote this program: #include <iostream> using namespace std; class sample { private: int age; float salary; char status;
3
1676
by: gorbulastic | last post by:
ok, i've created a typedef of somthing, and I put it through a for loop to put the entries into it. They are stored as an array of the typedef''s, with memory allocated via malloc command. For...
29
2736
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
3
1376
by: ciccio | last post by:
Hi, Recently some people I know stumbled upon a strange problem with the following piece of code. This is reduced as far as possible. The idea of the code is simple. There are four structs...
0
7041
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
6908
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
7084
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
4481
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...
0
2995
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
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
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 ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
181
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.