Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I'm using this class:

C#
[StructLayout(LayoutKind.Sequential, Pack = 1)]
    class THolidayTz
    {
        public ushort ID;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = 0)]
        public byte[][] HDate = new byte[4][]; // Error using Marshal.SizeOf
        public ushort TZID;
    };


When I use a Marshal.SizeOf:

C#
byte[] buf = new byte[Marshal.SizeOf(Holiday)];


I got this error:

You can not marshal type "THolidayTz 'as unmanaged structure: one can not calculate any shift or significant size.

I see the error happens when I use a byte[][] definition, using:

C#
public byte[]HDate = new byte[4];


I don't Have error, but i Need to use byte[][]...

Thanks.
Posted
Updated 9-Nov-11 5:25am
v2

For unmanaged types that do no easily map to .NET types, it is generally recommended to represent them as IntPtr's while marshalling and manually parse the data after the call completes. It would be helpful if you were to provide the unmanaged structure and function stub.
 
Share this answer
 
Comments
Geca 11-Nov-11 3:27am    
Can you show me the example for this case ?
Thanks
hi,
you need to do just like this,

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = 0)]
public byte[][] HDate;

it will work.

because, in C#.Net for unmanaged type you can not use new like this...
public byte[]HDate = new byte[4];
 
Share this answer
 
Comments
Geca 14-Nov-11 3:55am    
Hi. I Have the same error...

[StructLayout(LayoutKind.Sequential, Pack = 1)]
class THolidayTz
{
public ushort ID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = 0)]
public byte[][] HDate;
public ushort TZID;
};


Calling:

THolidayTz Holiday = new THolidayTz();
byte[] buf = new byte[Marshal.SizeOf(Holiday)]; // Error on Marshal.SizeOf

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900