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

Posted by Ofir on Stack Overflow See other posts from Stack Overflow or by Ofir
Published on 2011-01-30T15:06:45Z Indexed on 2011/01/30 15:26 UTC
Read the original article Hit count: 236

Filed under:
|
|

Hi all,

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about marshalling