Marshaling an array of structure

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have some problem in marshaling array of structure. I have to
exchange a structure of data with a C++ program. This structure data
contains a field which is an array of a custom type. I can't find the
correct sequence to declare the marshal attributes of my structures, I
always got an exception at runtime when marshaling my data.

Here an example of code, which gives me the exception "Type TypeData
can not be marshaled as an unmanaged structure; no meaningful size or
offset can be computed."

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication
{
unsafe class Class
{
[StructLayout(LayoutKind.Sequential)]
public struct TypeStruct
{
uint value1;
byte value2;
}

[StructLayout(LayoutKind.Sequential)]
public struct TypeData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
TypeStruct[] structData;
}

[STAThread]
static void Main (string[] args)
{
TypeData data = new TypeData();

try
{
Console.WriteLine (Marshal.SizeOf(data));
}
catch (Exception e)
{
Console.WriteLine (e.Message);
}
}
}
}


Please note that I'm running with the .dot 2003 version, thanks by
advance for any correcting suggestions.
 
Back
Top