Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

structures in structure

Status
Not open for further replies.

juhaka

Programmer
Nov 14, 2002
26
FI
Hi, again!

How can I make a new structure, which contains others structures?
I have two structures, which contains only native types (byte, uint) and I have a need for a new structure, which contains these my own datatypes (structures).
Code:
[ StructLayout( LayoutKind.Sequential )]
public struct MSettings_def
{
public byte MFormat;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=4)]
public byte[] MObjects;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=4)]
public byte[] ONumber;
public byte Brightness;
}

[ StructLayout( LayoutKind.Sequential )]
public struct Rparam_def
{
public byte UpperPoint;
public byte LowerPoint;
public byte MaxSpeed;
public byte MinSpeed;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=4)]
public uint[] Valves;
}

When I use Marshal.SizeOf(struct_object), I get the sizes of the structures, 10 and 20 bytes

Now, when I make a new structure:
Code:
public struct Param_def
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst=10)]
byte[] Name;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=80)]
byte[] Comment;
MSettings_def msf;
[MarshalAs( UnmanagedType.ByValArray, SizeConst=10)]
Rparam_def[] rpd;
}

If I call Marshal.SizeOf(param_def_object) -method, I get an error:Converter.Param_def can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed.


What's wrong in my Param_def -structure?

br. Juha Ka
 
I am getting similar error when including an array of structures in another structure:

"can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed. at System.Runtime.InteropServices.Marshal.SizeOf(Object structure) "

juhaka, did you ever figure out how to resolve this error?



 
I never resolved this error. I gave up with structs and did it with classes.

public class MSettings_def
{
public byte MFormat = 0;
public byte[] MObjects = new byte[4];
public byte[] ONumber = new byte[4];
public byte Brightness = 0;
}

public class Rparam_def
{
public byte UpperPoint = 0;
public byte LowerPoint = 0;
public byte MaxSpeed = 0;
public byte MinSpeed = 0;
public uint[] Valves = new uint[10];
}

public class Param_def
{
byte[] Name = new byte[20];
byte[] Comment = new byte[80];
MSettings_def msf = new MSetting_def();
Rparam_def[] rpd = new Rparam_def[10];
}

It was pity, because now I haven't be able to do type casting from c++ struct to c# struct....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top