Structure Marshalling Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roche72
    New Member
    • Oct 2008
    • 3

    #1

    Structure Marshalling Question

    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
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    Have you checked out this reference, at least until someone posts a more direct answer?
    Default Marshaling for Arrays

    Comment

    Working...