编组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.
我一直在一段时间封送处理问题所困扰。我有一个包含另一个结构的阵列结构,该平台的的Win CE 。我使用Visual Studio 2008和.NET CF 3.5
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.
中的代码:
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;
的
结构MY_SUPPORTED_DATA_RATES大小为 296字节在 C
这是我尝试将其转换成一个C#结构:
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;
};
下面我收到的大小为 304字节是 Marshal.SizeOf(my_supported_rates);
Here I am getting the size as 304 bytes using Marshal.SizeOf(my_supported_rates);
我曾尝试下面的东西没有任何成功:
I have tried the following things without any success:
- 添加和删除的MarshalAs各种attrubute元素my_supported_rates结构,如
ArraySubType = UnmanagedType.Struct
$属性b $ b - 我的IntPtr与所需的数据,我试图PTR转换为使用的代码
my_supported_rates =(my_supported_rates)
。但是适当的转换没有happend。
Marshal.PtrToStructure(PTR,my_supported_rates.GetType的结构() ); - 在博客和计算器这并不能证明$ B $ B对我有用的一些其他建议
- 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
推荐答案
您的翻译看对我好。运行在桌面上,而不是CE我发现,对于这些类型的
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;
};
这
Marshal.SizeOf(typeof(my_supported_rates)) == 296
所以它似乎在行政长官的PInvoke编组一些奇怪的。您可能需要通过这样做是为了迫使编组的手:
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;
};
也就是说,如果 LayoutKind.Explicit
和 FieldOffset
支持在CE。
如果他们不支持,那么你就需要手工元帅。您正在寻找 Marshal.AllocHGlobal
然后 Marshal.ReadByte
, Marshal.ReadInt16
等。
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.
这篇关于编组C结构在它的结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!