Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to marshal structs with unknown length string fields in C#

Tags:

I get an array of bytes, I need to unmarshal it to C# struct. I know the type of the struct, it has some strings fields. The strings in the byte array appears as so: two first bytes are the length of the string, then the string itself. I don;t know the length of the strings. I do know that its Unicode!

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class User
{
  int Id;//should be 1
  String UserName;//should be OFIR
  String FullName;//should be OFIR
}

the byte array looks like so: 00,00,01,00, 00,00,08,00, 4F,00,46,00,49,00,52,00, 00,00,08,00, 4F,00,46,00,49,00,52,00,

I also found this link with same problem unsolved: loading binary data into a structure

Thank you all, Ofir