2013-02-17 74 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(...)]屬性來指定正確的大小。