当前位置:编程学习 > C#/ASP.NET >>

新手请教一下C#下的结构体中嵌套结构体数组的问题

需要通过串口通信,对方是C++写的程序,通信结构体为
 
  [StructLayout(LayoutKind.Sequential)]
    public struct BT_BLADE_INFO
    {
        public byte state;              
      public byte reserved;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public byte[] cpu_tmp;          
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] bld_ip;    
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] bld_netmask;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public byte[] bld_gateway;

    }

 [StructLayout(LayoutKind.Sequential)]
    public struct BT_PACKAGE_DWON
    {
        public byte head;
        public byte package_type;
        public short verify_value;      
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public BT_BLADE_INFO[] blade_info;
    }

   现在需要把这个结构体转换为byte流进行通信,程序如下:
    BT_PACKAGE_DWON down_package = new BT_PACKAGE_DWON();
    int aaa = Marshal.SizeOf(down_package);

    现在总是报错
    A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll

    用Marshal.StructureToPtr也是一样的错误,我的程序是在wince5.0下执行,哪位知道这是为什么?或者说这个嵌套结构体该如何实现?

    谢谢!
--------------------编程问答-------------------- 对齐方式是可能的问题。
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct BT_BLADE_INFO
--------------------编程问答-------------------- 楼上的是什么意思?能说明白一些吗?

主要问题就是public BT_BLADE_INFO[] blade_info; 
这个是一个结构数组,如果是public BT_BLADE_INFO blade_info; 就没有任何问题
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,