Interop c++ struct in c# variable string size
<p>i transfer over ipc a struct from unmanaged into managed code.
Is it possible to transfer a string without set a constant size
or fill the rest auf the bytes into the last member of the struct ?</p>

<p><strong>c++ struct</strong></p>

<pre><code>typedef union
{
    struct
    { 
         int id;
         string Data;
    };
    char bytes[];
} SocketData;
</code></pre>

<hr>

<p><strong>c# struct</strong></p>

<pre><code>   [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct SocketData
    {
        public int id;
        public string Data;
    }
</code></pre>

<hr>

<p><strong>Convert bytes to struct</strong></p>

<pre><code>public static object RawDeserialize(byte[] rawData, int position, Type anyType)
{
    var rawsize = rawData.Length;
    //if (rawsize &gt; rawData.Length)
    //    return null;
    var buffer = Marshal.AllocHGlobal(rawsize);
    Marshal.Copy(rawData, position, buffer, rawsize);
    var retobj = Marshal.PtrToStructure(buffer, anyType);
    Marshal.FreeHGlobal(buffer);
    return retobj;
}
</code></pre>
