【问题标题】:Pinvoke for struct结构的 Pinvoke
【发布时间】:2013-02-17 19:08:08
【问题描述】:

我有以下结构定义:

#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;
}

但没有让它工作。

【问题讨论】:

  • 您的问题是如何翻译结构定义。如何填充其成员是完全不同的事情。
  • 很公平。将发布另一个问题。
  • @David,我在这里发布了一个新问题:stackoverflow.com/questions/14925478/…

标签: c# struct pinvoke


【解决方案1】:

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(...)] 属性来指定正确的大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多