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

Structure Marshalling Question

P: 3
I am having some trouble marshalling data between c++/C#

Here is the C++ code:
---------------------------------------------------------------------------------
typedef struct
{
......
......
}MyChildStruct;

typedef struct
{
......
......
MyChildStruct m_Children[10]; //This array of structures causes problems.
}MyParentStruct;


void SetData(MyParentStruct *pStructs, int numStructs)
{
....
}
void DoSomethingToData()
{
}


Here is the C# code
------------------------------------------------------------------------------------
struct MyChildStruct
{
......
......
};

struct MyParentStruct
{
......
......
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.Struct)]
MyChildStruct[] m_Children;
}

class MyDLL
{
[DllImport("MyDLL.dll")]
public static extern void SetData(IntPtr pStructs, int numStructs);

[DllImport("MyDLL.dll")]
public static extern void DoSomethingToData();
}
class Foo
{
private GCHandle m_GCHandle;
private MyParentStructs[] m_MyStructs = new MyParentStructs[100];

public Foo()
{
this.m_GCHandle = GCHandle.Alloc(this.m_MyStructs, GCHandleType.Pinned);

MyDll.SetData(m_GCHandle.AddrOfPinnedObject(), m_MyStructs.Length);

}
public void DoStuff()
{
MyDll.DoSomethingToData();
}
}

---------------------------------------------------------------------------

This used to work until I needed to add the array of structures to MyStruct2. As I don't have control over the unmanaged code, i need to resolve this.
I get a runtime error "Object contains non-primitive or non-blittable data." in Foo() when GCHandle.Alloc(...) is called.

thanks in advance
-steve
Dec 15 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 190
Have you checked out this reference, at least until someone posts a more direct answer?
Default Marshaling for Arrays
Dec 17 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.