Re: Class/struct and Marshal.SizeOf

From: SB (stormfire1_at_yahoo.com)
Date: 02/20/05

  • Next message: ARTMIC: "Calling Delphi DLL from inside C# Window program"
    Date: Sun, 20 Feb 2005 16:31:21 -0500
    
    

    First, thanks for looking at the code. I'm not sure why you are getting
    that error...but I will post the entire MainForm.cs file here which may
    help. This compiles and runs fine on my end. Note that some changes were
    made today by me but nothing substantial. One thing that may be different
    on your end is that I'm using VC# Express and the .NET Framework 2.0b (link
    below)...I should have mentioned that before.

    http://lab.msdn.microsoft.com/express/vcsharp/default.aspx

    Anyway, I've been googling this problem ALL DAY...and I'm beginning to
    believe this is a bug in the new framework. However, I'm still hoping that
    someone will show me otherwise.

    -sb

    #region Using directives
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    #endregion

    namespace MyTestCenter
    {
    partial class mainForm : Form
    {
        public mainForm()
    {
        InitializeComponent();

        richTextBox.AppendText("MyStruct size: " +
    Marshal.SizeOf(typeof(MyStruct)).ToString());//will show size of 6
        richTextBox.AppendText("\nMyClass size: " +
    Marshal.SizeOf(typeof(MyClass)).ToString());//will show size of 6
        richTextBox.AppendText("\nMyStructTestClass size: " +
    Marshal.SizeOf(typeof(MyStructTestClass)).ToString());//will show size of
    1536
        richTextBox.AppendText("\nMyClassTestClass size: " +
    Marshal.SizeOf(typeof(MyClassTestClass)).ToString());//will show size of
    1024
    }

    [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
    public struct MyStruct
    {
        public short a;
        public short b;
        public short c;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
    public class MyClass
    {
        public short a;
        public short b;
        public short c;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
    public class MyStructTestClass
    {
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
    SizeConst = 256)]
        public MyStruct[] dummy;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
    public class MyClassTestClass
    {
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
    SizeConst = 256)]
        public MyClass[] dummy;
    }
    }
    }


  • Next message: ARTMIC: "Calling Delphi DLL from inside C# Window program"

    Relevant Pages

    • Re: question relates to instance variable initialization
      ... I am unclear about the following two ways to initialize instance ... public class MyClass ...
      (comp.lang.java.programmer)
    • Re: XML Serializer
      ... CDATA to escape special characters, ... semantically, the meaning of XML remains exactly the same, and, unless ... public class MyClass ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Initializing variable style question
      ... public class MyClass ...     public MyClass ... To initialize to a non-zero-like value, ...
      (comp.lang.java.programmer)
    • RE: How to reference function in shared function?
      ... Public Class Class1 ... > Public Class myClass ... > shared member initializer without an explicit instance of the class. ... > I also tried this below the class declaration: ...
      (microsoft.public.dotnet.languages.vb)
    • Class/struct and Marshal.SizeOf (C#)
      ... [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] ... public class MyClass ... public class MyStructTestClass ... SizeConst = 256)] ...
      (microsoft.public.dotnet.framework.interop)

  • Quantcast