Marshalling ByValArray problem showing difference between full and compact framework
Sample code - Create a new PocketPC and Windows project, add a button, add in a using System.Runtime.InteropServices, and then this code:
[StructLayout(LayoutKind.Sequential)]
public struct structTEST
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public System.Byte[] ValId;
public System.UInt32 ValLong;
}
private void button1_Click(object sender, EventArgs e)
{
structTEST temp = new structTEST();
temp.ValId = new System.Byte[4];
temp.ValId[0] = (System.Byte)'H';
temp.ValId[1] = (System.Byte)'E';
temp.ValId[2] = (System.Byte)'L';
temp.ValId[3] = (System.Byte)'O';
temp.ValLong = (System.UInt32)0x12345678;
IntPtr buf = new IntPtr(0);
buf = Marshal.AllocCoTaskMem(100);
Marshal.StructureToPtr(temp, buf, false);
Byte[] b = new Byte[100];
Marshal.Copy(buf,b,0,20);
int i;
MessageBox.Show("Bytes...");
for (i=0;i<4;i++) {
String str = String.Format("byte {0:d} is {1:x}", i, b[ i ]);
MessageBox.Show(str);
}
MessageBox.Show("Long Bytes...");
for (i=4;i<8;i++) {
String str = String.Format("byte {0:d} is {1:x}", i, b[ i ]);
MessageBox.Show(str);
}
}PocketPC shows 4 bytes as a ptr then the long values, whereas PC version shows 48,45... etc as expected.
Thanks

