Interop service error: Cannot marshal field 'XXX' of type 'XXX': There is no marshaling support for


W

weixian_shen

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.dll")]
public extern static void unpack(
out mystruct str,
IntPtr buf);

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

}

[StructLayout(LayoutKind.Sequential)]
public struct struct_a
{
byte a;
}

[StructLayout(LayoutKind.Sequential)]
public struct struct_b
{
byte a;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=15)]
byte[] b;
}

[StructLayout(LayoutKind.Explicit)]
public struct struct_c
{
[FieldOffset(0)]
byte a;
[FieldOffset(1)]
struct_a b;
[FieldOffset(1)]
struct_b c;
}

[StructLayout(LayoutKind.Sequential)]
public struct mystruct
{
byte a;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)]
mystruct[] b;
[MarshalAs(UnmanagedType.ByValArray, 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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top