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