Search code examples
c#pinvokemarshallingdllimportunsafe

PInvoke style passing struct with member dynamic array of pointers


There appears to be multiple ways to do this, but the examples I've tried haven't worked for me.

I read somewhere that using unsafe pointers would be the way to go for more complex structures that require pointers. (sorry but I haven't been able to find the source again)

My latest attempt looks something like this:

unsafe public struct ComplexStruct {
    public Int32 NumSubStruct;
    public SubStruct*[] arr;
}

unsafe public static extern UInt32 DLL_Call(ref ComplexStruct p);

function {
unsafe {
    ComplexStruct p = new ComplexStruct();
    p.NumSubStruct = 2;
    p.arr= new SubStruct*[p.NumSubStruct];
    SubStruct p1 = new SubStruct();
    SubStruct p2 = new SubStruct();
    p.arr[0] = &p1;
    p.arr[1] = &p2;


    testLogHandlerHandle = DLL_Call(ref p);
}
}

I get an error which says that SubStruct cannot be marshaled (Signature is not interop compatible).

Is it possible to pass the object without marshaling? (the DLL should be loaded in the same process space as the C# application).

If not, what would be the simplest way to pass a copy of the object?

Note: changing the DLL is a non-issue. I am vaguely aware of some other solutions, such as C++/CLI, but I only have a relatively small number of structure types that need to be passed in such a way.

EDIT: A few notes:

  1. The array needs to by dynamic
  2. The actual structs a bit more complicated (there's 4 layers of nested structures like this) so I think adding them would detract from the question, but for completeness, here is how I would declare these in C++:

    struct ComplexStruct {
    
         unsigned int NumSubStruct;
    
         SubStruct** arr;
    
    };
    
    struct SubStruct {
    
         unsigned int NumSubStruct;
    
         //some more datamembers here as well
    
         SubSubStruct** arr;
    
    };
    

(the extra indirection isn't necessary, but I figured it might be useful to be able to declare the array of SubStruct pointers as IntPtrs)


Solution

  • You should use something like this and this.

    [StructLayout(LayoutKind.Sequential)]
    public struct ComplexStruct
    {
        public Int32 NumSubStruct;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NO_SUB_STRUCTS)]
        public SubStruct[] arr;
    }
    
    public static extern UInt32 DLL_Call(IntPtr p);
    

    Then marshal it with something like this:

    IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ComplexStruct)));
    
    try
    {
        // Copy the struct to unmanaged memory.
        Marshal.StructureToPtr(p, ptr, false);
    
        testLogHandlerHandle = DLL_Call(ptr);
    }
    finally
    {
        // Free the unmanaged memory.
        Marshal.FreeHGlobal(ptr);
    }
    

    Try to get rid of all the unsafe stuff, I doubt its necessary!