Marshall array of structures

后端 未结 1 1877
不思量自难忘°
不思量自难忘° 2021-02-07 18:17

I\'ve spent a lot of time to look for the solution but still don\'t find it out.

I have 2 classes:

[StructLayout(LayoutKind.Sequential)]
public class Res         


        
相关标签:
1条回答
  • 2021-02-07 18:59

    Change your classes to structs

    [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;
    }
    

    some where else:

    Marshal.SizeOf(typeof(CoverObject)) // it will return 52
    
    0 讨论(0)
提交回复
热议问题