0

I have the following struct definition:

#ifndef struct_emxArray_real_T
#define struct_emxArray_real_T
struct emxArray_real_T
{
    real_T *data;
    int32_T *size;
    int32_T allocatedSize;
    int32_T numDimensions;
    boolean_T canFreeData;
};
#endif /*struct_emxArray_real_T*/

and would like to use it in C# via PInvoke. The struct is meant to represent a matrix. Any C# struct code would be very much appreciated. Thanks!

Someone has made an attempt here:

[StructLayout(LayoutKind.Sequential, Size = 1)]
public unsafe struct mytype
{
public double* data;
public int* size;
public int allocatedSize;
public int numDimensions;
public bool canFreeData;
}

but did not get it to work.

cs0815
  • 16,751
  • 45
  • 136
  • 299

1 Answers1

2

C# structs do not support pointer types.

Instead, pointers must be ported as IntPtr; you can use the Marshal class to resolve the pointer.

Therefore, you should write something like

[StructLayout(LayoutKind.Sequential)]
public unsafe struct mytype
{
    public IntPtr data;
    public IntPtr size;
    public int allocatedSize;
    public int numDimensions;
    public bool canFreeData;
}

Check what size your boolean_T type is; you may need to use the [MarshalAs(...)] attribute to specify the correct size.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964