I want to transfer one of the structures from the DXGI library, but I ran into a problem. The target structure contains a pointer to an array of structures, in the second after, where the first indicates the size of this array. And I would not want to work directly with a pointer to an array, but with the help of a marshalller, do what is required.
I tried to do it like this:
[StructLayout(LayoutKind.Sequential)]
public struct PresentParameters
{
public uint DirtyRectsCount { get; set; }
[field: MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Struct, SizeParamIndex = 0)]
public Rect[] DirtyRects { get; set; }
[field: MarshalAs(UnmanagedType.LPStruct)]
public Rect ScrollRect { get; set; }
[field: MarshalAs(UnmanagedType.LPStruct)]
public Point ScrollOffset { get; set; }
}
But the original structure looks like this:
typedef struct DXGI_PRESENT_PARAMETERS
{
UINT DirtyRectsCount;
/* [annotation] */
_Field_size_full_opt_(DirtyRectsCount) RECT *pDirtyRects;
RECT *pScrollRect;
POINT *pScrollOffset;
} DXGI_PRESENT_PARAMETERS;
Is it possible to do this, or is it worth describing a custom marshaller who would do this?
IntPtr(exceptUINT DirtyRectsCount), then usevar handle = GCHandle.Alloc(ObjectArray[0], GCHandleType.Pinned);(whereObjectArrayis your array ofstructs) and assignDirtyRects = handle.AddrOfPinnedObject();. Of course,handle.Free();after.