简体   繁体   English

Pinvoke for struct

[英]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. 并希望通过PInvoke在C#中使用它。 The struct is meant to represent a matrix. 结构旨在表示矩阵。 Any C# struct code would be very much appreciated. 任何C#结构代码都将非常感激。 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. C#结构不支持指针类型。

Instead, pointers must be ported as IntPtr ; 相反,指针必须移植为IntPtr ; you can use the Marshal class to resolve the pointer. 您可以使用Marshal类来解析指针。

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; 检查boolean_T类型的大小; you may need to use the [MarshalAs(...)] attribute to specify the correct size. 您可能需要使用[MarshalAs(...)]属性来指定正确的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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