2

I'm trying to marshal the following structure:

[StructLayout(LayoutKind.Sequential)]
public struct LogonTelegramStruct
{
    [MarshalAs(UnmanagedType.I2)]
    public ushort length;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
    public string name;

    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.ByValTStr,
               Size = (length-21)/5 )]
    public string[] points;
}

How can I marshal the points array? The length of a single point is always 5 bytes. The length field gives the length of the rest of the structure. Therefore the points array size is (length-21)/5. Is it possible to marshal this without looping through each point?

Rup
  • 33,765
  • 9
  • 83
  • 112
Benedikt
  • 109
  • 2
  • 4
  • 2
    You cannot get a useful answer when you don't post the C struct declaration. What you have is very unlikely to be close. Google "c# marshalling a variable length struct" to get somewhere. – Hans Passant Jun 16 '16 at 16:35
  • I have no C declaration, since it is a TCP packet. I was reading this: http://stackoverflow.com/questions/21798986/marshal-a-c-struct-containing-a-variable-length-array ,but here the length is specified directly. – Benedikt Jun 16 '16 at 16:48
  • 3
    Oh Lord, don't do this. Use the BinaryReader class. – Hans Passant Jun 16 '16 at 16:49
  • Thanks, I will try it like that. – Benedikt Jun 16 '16 at 17:09

0 Answers0