马歇尔结构数组 [英] Marshall array of structures
本文介绍了马歇尔结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我花了很多时间寻找解决方案,但仍然找不到答案.
I've spent a lot of time to look for the solution but still don't find it out.
我有2个课程:
[StructLayout(LayoutKind.Sequential)]
public class Result
{
public int Number;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string Name;
public int Size;
}
[StructLayout(LayoutKind.Sequential)]
public class CoverObject
{
public int NumOfResults;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 4)]
public Result[] Results;
}
我期望命令 Marshal.SizeOf(typeof(CoverObject))
将返回52,但不会返回20,因此,我以后使用的所有marshall和unmarshall均无法正常工作.
My expectation that the command Marshal.SizeOf(typeof(CoverObject))
will return 52, but not, it's just 20. Thus, all of marshall and unmarshall that I use later are not working.
似乎只计算Result类中的第一个成员(数字).我做错了什么吗?
Seeming it only counts the first member (Number) in Result class. Did I do anything wrong?
推荐答案
将类更改为结构
[StructLayout(LayoutKind.Sequential)]
public struct Result
{
public int Number;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string Name;
public int Size;
}
[StructLayout(LayoutKind.Sequential)]
public struct CoverObject
{
public int NumOfResults;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 4)]
public Result[] Results;
}
其他地方:
Marshal.SizeOf(typeof(CoverObject)) // it will return 52
这篇关于马歇尔结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文