c# marshaling dynamic-length string

Posted by mitsky on Stack Overflow See other posts from Stack Overflow or by mitsky
Published on 2010-05-28T13:19:37Z Indexed on 2010/05/28 13:21 UTC
Read the original article Hit count: 159

Filed under:
|
|
|

i have a struct with dynamic length

   [StructLayout(LayoutKind.Sequential, Pack = 1)]
   struct PktAck
   {
      public Int32 code;
      [MarshalAs(UnmanagedType.LPStr)]
      public string text;
   }

when i'm converting bytes[] to struct by:

        GCHandle handle = GCHandle.Alloc(value, GCHandleType.Pinned);
        stru = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
        handle.Free();

i have a error, because size of struct less than size of bytes[] and "string text" is pointer to string...

how can i use dynamic strings? or i can use only this:

  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]

© Stack Overflow or respective owner

Related posts about c#

Related posts about string