locked
Declaration of a user defined struct array of fixed length in struct

    Question

  • Hi!

    I'm trying to map incomming udp messages from C legacy code to the same structure C# code. What I'm struggaling with is how to declare a user defined struct array inside another struct. I want a InputMap array of certain size say 10 elements or 15 elements.

    I have tried using
    [MarshalAs(UnmannagedType.ByValArray, SizeConst=10)]
    InputMap[] input;


    Buthist doesnt seem to work...

    here is the struct code:

     [StructLayout(LayoutKind.Sequential)]
        struct InputMap
        {
          public  byte      state;         
          public  byte      change;        
          public  short     inputCount;   
          public  short     channel;         
          public  short     bank;          
          public  byte      isOnExtBoard;   
          public  byte      inverted;       
        }   
    
        struct DataMsg
        {
            
            public InputMap input[17]; //I want to be able to specify the size of the struct array 
        }
    
        struct AckMsg
        {
            public short     MsgNbr;       
        }
    
        //Simulate C union
        [StructLayout(LayoutKind.Explicit)]
        struct Message 
        {
            [FieldOffset(0)]    public DataMsg dataMsg;
            [FieldOffset(0)]    public AckMsg ackMsg;   
        }
    
        [StructLayout(LayoutKind.Sequential,Pack=1)]
        struct MsgPack
        {
            public byte     deviceID;    
            public byte     type;        
            public short    msgNbr;      
            public uint     magicNumber; 
            public Message   data;        
        }   
    Thanks!
    Wednesday, February 10, 2010 11:06 AM

Answers

  • I'm trying to map incomming udp messages from C legacy code to the same structure C# code. What I'm struggaling with is how to declare a user defined struct array inside another struct. I want a InputMap array of certain size say 10 elements or 15 elements.

    I have tried using
    [MarshalAs(UnmannagedType.ByValArray, SizeConst=10)]
    
    InputMap[] input;
    
    


    Buthist doesnt seem to work...


    It works. The problem is with the FieldOffset in the Message structure. A workaround is to make the AckMsg.MsgNbr an array of 1 element.

    struct DataMsg
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public InputMap[] input;
    }
    
    struct AckMsg
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
        public short[] MsgNbr;
    }
    • Marked as answer by Bin-ze Zhao Monday, February 15, 2010 9:29 AM
    Wednesday, February 10, 2010 1:21 PM

All replies

  • Not really possible I think.

    You could change:
    public
    InputMap input[17];
    to:
    public InputMap[] input = new InputMap[17];

    But then you have the problem that you get a reference to another array somewhere on the heap. But in this case you need the array to be part of the same struct.
    Wednesday, February 10, 2010 11:56 AM
  • I'm trying to map incomming udp messages from C legacy code to the same structure C# code. What I'm struggaling with is how to declare a user defined struct array inside another struct. I want a InputMap array of certain size say 10 elements or 15 elements.

    I have tried using
    [MarshalAs(UnmannagedType.ByValArray, SizeConst=10)]
    
    InputMap[] input;
    
    


    Buthist doesnt seem to work...


    It works. The problem is with the FieldOffset in the Message structure. A workaround is to make the AckMsg.MsgNbr an array of 1 element.

    struct DataMsg
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public InputMap[] input;
    }
    
    struct AckMsg
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
        public short[] MsgNbr;
    }
    • Marked as answer by Bin-ze Zhao Monday, February 15, 2010 9:29 AM
    Wednesday, February 10, 2010 1:21 PM