簡體   English   中英

Pinvoke for struct

[英]Pinvoke for struct

我有以下結構定義:

#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#結構代碼都將非常感激。 謝謝!

有人在這里嘗試

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

但沒有得到它的工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM