Marshalling C Struct with array of structures in it

后端 未结 1 1402
囚心锁ツ
囚心锁ツ 2021-01-22 22:41

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 structur

相关标签:
1条回答
  • 2021-01-22 23:06

    Your translations look good to me. Running on desktop rather than CE I find that, for these types

    [StructLayout(LayoutKind.Sequential)]
    public struct dot11Rate
    {
        public uint rate;
        public byte mode;
    };
    
    [StructLayout(LayoutKind.Sequential)]
    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;
    };
    

    that

    Marshal.SizeOf(typeof(my_supported_rates)) == 296
    

    So it would seem to be something odd in the CE pinvoke marshaller. You might need to force the hand of the marshaller by doing this:

    [StructLayout(LayoutKind.Explicit, Size=296)]
    public struct my_supported_rates
    {       
        [FieldOffset(0)]
        public ushort n_rates;
        [FieldOffset(4)]
        [MarshalAs(UnmanagedType.ByValArray,SizeConst = 36)]
        public dot11Rate[] srates;
        [FieldOffset(292)]
        public byte isSet;
        [FieldOffset(293)]
        public byte no_of_HTStreams;
    };
    

    That is, if LayoutKind.Explicit and FieldOffset are supported on CE.

    If they are not supported then you'll need to marshal by hand. You are looking for Marshal.AllocHGlobal and then Marshal.ReadByte, Marshal.ReadInt16 and so on.

    0 讨论(0)
提交回复
热议问题