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(MyParen tStruct *pStructs, int numStructs)
{
....
}
void DoSomethingToDa ta()
{
}
Here is the C# code
------------------------------------------------------------------------------------
struct MyChildStruct
{
......
......
};
struct MyParentStruct
{
......
......
[MarshalAsAttrib ute(UnmanagedTy pe.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.S truct)]
MyChildStruct[] m_Children;
}
class MyDLL
{
[DllImport("MyDL L.dll")]
public static extern void SetData(IntPtr pStructs, int numStructs);
[DllImport("MyDL L.dll")]
public static extern void DoSomethingToDa ta();
}
class Foo
{
private GCHandle m_GCHandle;
private MyParentStructs[] m_MyStructs = new MyParentStructs[100];
public Foo()
{
this.m_GCHandle = GCHandle.Alloc( this.m_MyStruct s, GCHandleType.Pi nned);
MyDll.SetData(m _GCHandle.AddrO fPinnedObject() , m_MyStructs.Len gth);
}
public void DoStuff()
{
MyDll.DoSomethi ngToData();
}
}
---------------------------------------------------------------------------
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
Here is the C++ code:
---------------------------------------------------------------------------------
typedef struct
{
......
......
}MyChildStruct;
typedef struct
{
......
......
MyChildStruct m_Children[10]; //This array of structures causes problems.
}MyParentStruct ;
void SetData(MyParen tStruct *pStructs, int numStructs)
{
....
}
void DoSomethingToDa ta()
{
}
Here is the C# code
------------------------------------------------------------------------------------
struct MyChildStruct
{
......
......
};
struct MyParentStruct
{
......
......
[MarshalAsAttrib ute(UnmanagedTy pe.ByValArray, SizeConst = 10, ArraySubType = UnmanagedType.S truct)]
MyChildStruct[] m_Children;
}
class MyDLL
{
[DllImport("MyDL L.dll")]
public static extern void SetData(IntPtr pStructs, int numStructs);
[DllImport("MyDL L.dll")]
public static extern void DoSomethingToDa ta();
}
class Foo
{
private GCHandle m_GCHandle;
private MyParentStructs[] m_MyStructs = new MyParentStructs[100];
public Foo()
{
this.m_GCHandle = GCHandle.Alloc( this.m_MyStruct s, GCHandleType.Pi nned);
MyDll.SetData(m _GCHandle.AddrO fPinnedObject() , m_MyStructs.Len gth);
}
public void DoStuff()
{
MyDll.DoSomethi ngToData();
}
}
---------------------------------------------------------------------------
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
Comment