I referred the similar questions in this forum but didn't get the solution for my problem.
I have been struggling with marshaling problem for a while. I have a structure that contains an array of another structure, The platform is Win CE. I am using Visual Studio 2008 and .NET CF 3.5.
The code:
C Structures:
struct dot11Rate
{
unsigned int rate;
unsigned char mode;
};
typedef struct my_supported_rates
{
unsigned short n_rates;
struct dot11Rate srates[36];
unsigned char isSet;
unsigned char no_of_HTStreams;
}MY_SUPPORTED_DATA_RATES;
Size of the struct MY_SUPPORTED_DATA_RATES is 296 bytes in C
This is my attempt to convert it into a C# struct:
C# Converted:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct dot11Rate
{
public uint rate;
public byte mode; /* HT=1, non-HT=0*////
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct my_supported_rates
{
public ushort n_rates;
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 36)]
public dot11Rate[] srates;
public byte isSet;
public byte no_of_HTStreams;
};
Here I am getting the size as 304 bytes using Marshal.SizeOf(my_supported_rates);
I have tried the following things without any success:
- Adding and removing various attrubute elements in MarshalAs attribute in my_supported_rates struct such as
ArraySubType = UnmanagedType.Struct
- I have Intptr with required data and I tried to convert ptr to the struct using the code
my_supported_rates = (my_supported_rates) Marshal.PtrToStructure(ptr,my_supported_rates.GetType());
. But proper conversion is not happend. - Some other suggestions on blogs and StackOverflow which didn't prove useful to me