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?

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. Commented Jun 16, 2016 at 16:35
  • I have no C declaration, since it is a TCP packet. I was reading this: stackoverflow.com/questions/21798986/… ,but here the length is specified directly. Commented Jun 16, 2016 at 16:48
  • 3
    Oh Lord, don't do this. Use the BinaryReader class. Commented Jun 16, 2016 at 16:49
  • Thanks, I will try it like that. Commented Jun 16, 2016 at 17:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.