If there is a better NG for compact framework interop then please let me know.
I am having a few issues marshalling structs to unmanaged memory whilst
trying to acheive the correct layout. Its a shame that the Pack parameter is
not supported in the compact framework.
I have a simple struct:
[StructLayout(LayoutKind.Sequential)]
public struct STest
{
public byte ser0;
public byte ser1;
public byte ser2;
public byte testByte;
public ushort testUshort;
public uint testuint;
}
when used like so:
STest test;
test.ser0 = 1;
test.ser1 = 2;
test.ser2 = 3;
test.testByte = 0x11;
test.testUshort = 0x2222;
test.testuint = 0x33333333;
which when marshalled looks like the following in memory.
0x00047420 01 02 03 11 22 22 00 00 33 33 33 33
I have treid using size=10, but get a TypeLoadException. If I try lay the
struct out explicitly, I get the same error. What am I missing?
Short of walking the struct and manually marshalling it is there any way to
force the marshaller to use byte size packing?
This may not be the best way to do this, but I think it might work. I don't
have a native method to try this out on, so I didn't test this. It does
compile though :). Please let me know if it works.
[StructLayout(LayoutKind.Sequential)]
public struct STest
{
private byte ser0;
private byte ser1;
private byte ser2;
private byte testByte;
private byte testUshortLow;
private byte testUshortHigh;
private uint testuint;
public byte Ser0
{
get
{
return ser0;
}
set
{
ser0 = value;
}
}
public byte Ser1
{
get
{
return ser1;
}
set
{
ser1 = value;
}
}
public byte Ser2
{
get
{
return ser2;
}
set
{
ser2 = value;
}
}
public byte TestByte
{
get
{
return testByte;
}
set
{
testByte = value;
}
}
public ushort TestUshort
{
get
{
return BitConverter.ToUInt16(new byte[] {
this.testUshortLow, this.testUshortHigh }, 0);
}
set
{
byte[] bytes = BitConverter.GetBytes(value);
this.testUshortLow = bytes[0];
this.testUshortHigh = bytes[1];
}
}
public uint Testuint
{
get
{
return testuint;
}
set
{
testuint = value;
}
}
public STest(byte ser0, byte ser1, byte ser2, byte testByte, ushort
testUshort, uint testuint)
{
this.ser0 = ser0;
this.ser1 = ser1;
this.ser2 = ser2;
this.testByte = testByte;
byte[] bytes = BitConverter.GetBytes(testUshort);
this.testUshortLow = bytes[0];
this.testUshortHigh = bytes[1];
this.testuint = testuint;
}
}
> If there is a better NG for compact framework interop then please let me know.
>
[quoted text clipped - 31 lines]
> Short of walking the struct and manually marshalling it is there any way to
> force the marshaller to use byte size packing?
The Real Andy - 17 Nov 2006 21:47 GMT
>This may not be the best way to do this, but I think it might work. I don't
>have a native method to try this out on, so I didn't test this. It does
>compile though :). Please let me know if it works.
Thanks, will give it a try on Monday when I get back to work.
You will have to excuse me for being cynical, but I think MS has
really screwed this one up. I normally talk up all these technologies,
but exluding pack=1 on an embedded platform is just blatently stupid.
IHMO the compact framework is probably useful only for writing dinky
little GUI toys for smartphones and PDA's. It simply is not cut out
for doing any industrial strength application.
I have been allocated a month to investigate the whether the compact
framework will suit our requirements. At this stage my recommendation
is heading down the unmanaged c++ path.
> [StructLayout(LayoutKind.Sequential)]
> public struct STest
[quoted text clipped - 131 lines]
>> Short of walking the struct and manually marshalling it is there any way to
>> force the marshaller to use byte size packing?