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

P/Invoke with struct array, failed

P: n/a
Hi group,

I'v got something wrong with P/Invoke. If I have two structs defined like
this:
typedef struct{
DWORD F1;
}A, *PA;

typedef struct{
A Data[8];
}B, *PB;

Then I define two structs in C# to interop:

[StructLayout(LayoutKind.Sequential)]
struct A{
public uint F1;
}

[StructLayout(LayoutKind.Sequential)]
struct B{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public A[] Data;
}

It seems very easy but it does not works. The following is my test code.

in VC:

extern "C" DLLTEST_API void StructTest(B b)
{
A* a = b.Data;
for(int i = 0;i < 8;i++)
{
cout << a[i].F1 << endl;
}
}

in C#:

[DllImport(TestDllPath)]
static extern void StructTest(B b);

[STAThread]
static void Main()
{
StructTest(new B());
}

The exception is:
Can not marshal field Data of type B: This type can not be marshaled as a
structure field.

I don't know why. Any one can help me? Thank you very much.

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


P: n/a

"Xia Wei" <ip*****@gmail.com> wrote in message
news:d4***********@mail.cn99.com...
Hi group,

I'v got something wrong with P/Invoke. If I have two structs defined like
this:
typedef struct{
DWORD F1;
}A, *PA;

typedef struct{
A Data[8];
}B, *PB;

Then I define two structs in C# to interop:

[StructLayout(LayoutKind.Sequential)]
struct A{
public uint F1;
}

[StructLayout(LayoutKind.Sequential)]
struct B{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public A[] Data;
}

It seems very easy but it does not works. The following is my test code.

in VC:

extern "C" DLLTEST_API void StructTest(B b)
{
A* a = b.Data;
for(int i = 0;i < 8;i++)
{
cout << a[i].F1 << endl;
}
}

in C#:

[DllImport(TestDllPath)]
static extern void StructTest(B b);

[STAThread]
static void Main()
{
StructTest(new B());
}

The exception is:
Can not marshal field Data of type B: This type can not be marshaled as a
structure field.

I don't know why. Any one can help me? Thank you very much.


hmm... because the v1.x marshaler can't handle structures that contain
array's of struct's.
One way to solve this is to flatten your structure like:

[StructLayout(LayoutKind.Sequential)]
struct A{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public uint[] F1;
}

Willy.
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.