By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

Help with interop service, marshalling

P: n/a
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)] // tried fieldoffset(4) as well for alignment
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.

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
>[StructLayout(LayoutKind.Explicit)]
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;
}
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(UnmanagedType.ByValArray, SizeConst=15)]
public byte[] b; // struct_b.b
}

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


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

http://www.dotnetinterop.com/faq/?q=...ithStructArray

Mattias

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

This discussion thread is closed

Replies have been disabled for this discussion.