I
indiekiduk
Hi, does anyone why the code below would produce the following result?
2.0.6129
56
1.0.4292
48
I know that CF2 has better support for struct marshalling due to it
including the FieldOffset and Pack attributes which CF1 lacks. However
my application is in CF1 for WM5 end user niceness. However, since WM6
now includes CF2 and not CF1 my application isn't compatible because
of this problem. Is there anyway I can fix this sample code to run on
both CF1 and CF2 properly?
I should point out the resulting problem is that when you access the
Marshal from native to this struct in CF2 the data gets mangled
because some of the offsets are wrong. I understand packing and I
appreciate that the MAC struct gets filled up to 8 bytes, thats ok,
but what is CF2 doing different?
(Note if I install CF1 onto the WM6 PDA gives the correct value of 48,
but I'm trying to avoid doing that. The fact that Visual Studio does
it automatically might cause headaches for some developers when they
release.)
#region Using directives
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
#endregion
using System.Runtime.InteropServices;
namespace StructTester
{
/// <summary>
/// Summary description for form.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Label label1;
private MenuItem menuItem1;
private Label label2;
/// <summary>
/// Main menu for the form.
/// </summary>
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
InitializeComponent();
label2.Text = Environment.Version.ToString();
Test t = new Test();
label1.Text = Marshal.SizeOf(t).ToString();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.label1 = new System.Windows.Forms.Label();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.label2 = new System.Windows.Forms.Label();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
//
// label1
//
this.label1.Location = new System.Drawing.Point(3, 40);
this.label1.Size = new System.Drawing.Size(152, 22);
this.label1.Text = "label1";
//
// menuItem1
//
this.menuItem1.Text = "Close";
this.menuItem1.Click += new
System.EventHandler(this.menuItem1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(3, 3);
this.label2.Size = new System.Drawing.Size(152, 22);
this.label2.Text = "label2";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(176, 180);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void menuItem1_Click(object sender, EventArgs e)
{
Close();
}
}
//cf1 48
//cf2 56
[StructLayout(LayoutKind.Sequential)]
struct Test
{
int a;
MAC m;
SSID s;
}
[StructLayout(LayoutKind.Sequential)]
struct MAC
{
byte b1, b2, b3, b4, b5, b6;
}
[StructLayout(LayoutKind.Sequential)]
public struct SSID
{
public uint SsidLength; // length of SSID field below,
in bytes;
// this can be zero.
// SSID array of 32 UCHAR
public long Ssid_0;
public long Sssid_1, Ssid_2, Ssid_3; // SSID
information field
}
}
2.0.6129
56
1.0.4292
48
I know that CF2 has better support for struct marshalling due to it
including the FieldOffset and Pack attributes which CF1 lacks. However
my application is in CF1 for WM5 end user niceness. However, since WM6
now includes CF2 and not CF1 my application isn't compatible because
of this problem. Is there anyway I can fix this sample code to run on
both CF1 and CF2 properly?
I should point out the resulting problem is that when you access the
Marshal from native to this struct in CF2 the data gets mangled
because some of the offsets are wrong. I understand packing and I
appreciate that the MAC struct gets filled up to 8 bytes, thats ok,
but what is CF2 doing different?
(Note if I install CF1 onto the WM6 PDA gives the correct value of 48,
but I'm trying to avoid doing that. The fact that Visual Studio does
it automatically might cause headaches for some developers when they
release.)
#region Using directives
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
#endregion
using System.Runtime.InteropServices;
namespace StructTester
{
/// <summary>
/// Summary description for form.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Label label1;
private MenuItem menuItem1;
private Label label2;
/// <summary>
/// Main menu for the form.
/// </summary>
private System.Windows.Forms.MainMenu mainMenu1;
public Form1()
{
InitializeComponent();
label2.Text = Environment.Version.ToString();
Test t = new Test();
label1.Text = Marshal.SizeOf(t).ToString();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.label1 = new System.Windows.Forms.Label();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.label2 = new System.Windows.Forms.Label();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
//
// label1
//
this.label1.Location = new System.Drawing.Point(3, 40);
this.label1.Size = new System.Drawing.Size(152, 22);
this.label1.Text = "label1";
//
// menuItem1
//
this.menuItem1.Text = "Close";
this.menuItem1.Click += new
System.EventHandler(this.menuItem1_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(3, 3);
this.label2.Size = new System.Drawing.Size(152, 22);
this.label2.Text = "label2";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(176, 180);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void menuItem1_Click(object sender, EventArgs e)
{
Close();
}
}
//cf1 48
//cf2 56
[StructLayout(LayoutKind.Sequential)]
struct Test
{
int a;
MAC m;
SSID s;
}
[StructLayout(LayoutKind.Sequential)]
struct MAC
{
byte b1, b2, b3, b4, b5, b6;
}
[StructLayout(LayoutKind.Sequential)]
public struct SSID
{
public uint SsidLength; // length of SSID field below,
in bytes;
// this can be zero.
// SSID array of 32 UCHAR
public long Ssid_0;
public long Sssid_1, Ssid_2, Ssid_3; // SSID
information field
}
}