Help with interop service, marshalling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • weixian_shen@yahoo.com

    Help with interop service, marshalling

    I'm trying to call my DLL written in C, and got the error: "Cannot
    marshal field 'b' of type 'mystruct': There is no marshaling support
    for this type." The 2 functions in the DLL are:

    void unpack(mystruct * str, void* in_buf);
    void pack(mystruct* str, void* out_buf);

    The data structure is as follows:

    typedef struct
    {
    byte a;
    } struct_a;

    typedef struct
    {
    byte a;
    byte b[15];
    } struct_b;

    typedef struct
    {
    byte a;
    union
    {
    struct_a b;
    struct_b c;
    };
    } struct_c;

    typedef struct
    {
    byte a;
    struct_c b[15];
    struct_c c[15];
    } mystruct;

    My type definition in C# is:
    public class PRLCodec
    {
    [DllImport("my.d ll")]
    public extern static void unpack(
    out mystruct str,
    IntPtr buf);

    [DllImport("my.d ll")]
    public extern static void pack(
    ref mystruct str,
    IntPtr buf);
    }

    [StructLayout(La youtKind.Sequen tial)]
    public struct struct_a
    {
    byte a;
    }

    [StructLayout(La youtKind.Sequen tial)]
    public struct struct_b
    {
    byte a;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=15)]
    byte[] b;
    }

    [StructLayout(La youtKind.Explic it)]
    public struct struct_c
    {
    [FieldOffset(0)]
    byte a;
    [FieldOffset(1)] // tried fieldoffset(4) as well for alignment
    struct_a b;
    [FieldOffset(1)]
    struct_b c;
    }

    [StructLayout(La youtKind.Sequen tial)]
    public struct mystruct
    {
    byte a;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst = 15)]
    mystruct[] b;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst = 15)]
    mystruct[] c;
    }

    The code compiles but gives the above error. What could be wrong? I
    tried to read documents on interop service, but it seemed overwhelming
    to a new .NET programmer. Any help is appreciated.

  • Mattias Sjögren

    #2
    Re: Help with interop service, marshalling

    >[StructLayout(La youtKind.Explic it)][color=blue]
    >public struct struct_c
    >{
    > [FieldOffset(0)]
    > byte a;
    > [FieldOffset(1)] // tried fieldoffset(4) as well for alignment
    > struct_a b;
    > [FieldOffset(1)]
    > struct_b c;
    >}[/color]

    It would be easier to forget about the union and just declare this as

    public struct struct_c
    {
    public byte a;
    public byte a2; // struct_a.a or struct_b.a depending on context
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=15)]
    public byte[] b; // struct_b.b
    }

    [color=blue]
    >[StructLayout(La youtKind.Sequen tial)]
    >public struct mystruct
    >{
    > byte a;
    > [MarshalAs(Unman agedType.ByValA rray, SizeConst = 15)]
    > mystruct[] b;
    > [MarshalAs(Unman agedType.ByValA rray, SizeConst = 15)]
    > mystruct[] c;
    >}[/color]

    I assume you meant

    struct_c[] b;

    instead of

    mystruct[] c;

    because the struct can obviously not contain an inline array of
    itself. Anyway, it's still not supported by the marshaler. See





    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    Working...