简体   繁体   中英

Pinvoke for struct

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM