简体   繁体   English

编组具有结构数组的C结构

[英]Marshalling C Struct with array of structures in it

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 . 我有一个结构,其中包含另一个结构的数组,该平台是Win CE I am using Visual Studio 2008 and .NET CF 3.5. 我正在使用Visual Studio 2008和.NET CF 3.5。

The code: 编码:

C Structures: C结构:

 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 结构MY_SUPPORTED_DATA_RATES的大小在C语言中为296个字节

This is my attempt to convert it into a C# struct: 这是我尝试将其转换为C#结构:

C# Converted: C#转换为:

    [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); 在这里,我使用Marshal.SizeOf(my_supported_rates);获得304字节的大小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 在my_supported_rates结构的MarshalAs属性中添加和删除各种attrubute元素,例如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()); 我有需要数据的Intptr,我尝试使用代码my_supported_rates = (my_supported_rates) Marshal.PtrToStructure(ptr,my_supported_rates.GetType());将ptr转换为结构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 博客和StackOverflow上的其他一些建议对我没有帮助

Your translations look good to me. 您的翻译对我来说看起来不错。 Running on desktop rather than CE I find that, for these types 在台式机而不是CE上运行,我发现对于这些类型

[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. 因此,在CE拼字游戏编组中似乎有些奇怪。 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. 也就是说,CE上是否支持LayoutKind.ExplicitFieldOffset

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. 您正在寻找Marshal.AllocHGlobal ,然后寻找Marshal.ReadByteMarshal.ReadInt16等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM