2013-02-17 32 views
0

我有以下结构定义:的PInvoke的结构

#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*/ 

,并想通过的PInvoke使用它在C#中。该结构意味着代表一个矩阵。任何C#结构代码将非常感激。谢谢!

有人已经作出了尝试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; 
} 

,但没有得到它的工作。

+0

你的问题是如何翻译结构定义。如何填充其成员是完全不同的问题。 – 2013-02-17 19:56:54

+0

够公平的。将张贴另一个问题。 – cs0815 2013-02-17 19:59:50

+0

@David,我在这里发布了一个新问题:http://stackoverflow.com/questions/14925478/initialisation-of-struct-for-pinvoke – cs0815 2013-02-17 20:24:16

回答

2

C#结构不支持指针类型。

相反,指针必须移植为IntPtr;您可以使用Marshal类来解析指针。

因此,你应该写类似

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

检查什么大小你boolean_T类型;您可能需要使用[MarshalAs(...)]属性来指定正确的大小。