c# - C# 和非托管 C 库之间的互操作

标签 c# c interop marshalling

我在 DLL 中有一个小型 C 库,我需要调用它的一些方法。

它使用指针和一些结构,但在其他方面非常简单。问题是我对 .NET 与非托管世界的互操作知之甚少,到目前为止我的尝试一直遇到内存访问冲突异常(可能是因为我没有得到完全正确的指针)。

谁能给我一些关于解决这个问题的最佳方法的建议(哦,双关语!)?

谢谢

extern vconfig_t *Pobsopen(Ppoly_t ** obstacles, int n_obstacles);


extern int Pobspath(vconfig_t * config, Ppoint_t p0, int poly0,
            Ppoint_t p1, int poly1,
            Ppolyline_t * output_route);

extern void Pobsclose(vconfig_t * config);

struct vconfig_t {
    int Npoly;
    int N;
    Ppoint_t *P;
    int *start;
    int *next;
    int *prev;
};

typedef struct Ppoly_t {
    Ppoint_t *ps;
    int pn;
} Ppoly_t;

typedef Ppoly_t Ppolyline_t;

typedef struct Pxy_t {
    double x, y;
} Pxy_t;

typedef struct Pxy_t Ppoint_t;
typedef struct Pxy_t Pvector_t;

最佳答案

您应该查看 tool在本 MSDN 杂志中给出 article可以将 C 代码片段转换为 C# P/Invoke 签名,当然还有帖子。

为您的代码片段运行该工具可以得到:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct vconfig_t {

    /// int
    public int Npoly;

    /// int
    public int N;

    /// Ppoint_t*
    public System.IntPtr P;

    /// int*
    public System.IntPtr start;

    /// int*
    public System.IntPtr next;

    /// int*
    public System.IntPtr prev;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Ppoly_t {

    /// Ppoint_t*
    public System.IntPtr ps;

    /// int
    public int pn;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Pxy_t {

    /// double
    public double x;

    /// double
    public double y;
}

public partial class NativeMethods {

    /// Return Type: vconfig_t*
    ///obstacles: Ppoly_t**
    ///n_obstacles: int
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="Pobsopen")]
public static extern  System.IntPtr Pobsopen(ref System.IntPtr obstacles, int n_obstacles) ;


    /// Return Type: int
    ///config: vconfig_t*
    ///p0: Ppoint_t->Pxy_t
    ///poly0: int
    ///p1: Ppoint_t->Pxy_t
    ///poly1: int
    ///output_route: Ppolyline_t*
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="Pobspath")]
public static extern  int Pobspath(ref vconfig_t config, Pxy_t p0, int poly0, Pxy_t p1, int poly1, ref Ppoly_t output_route) ;


    /// Return Type: void
    ///config: vconfig_t*
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="Pobsclose")]
public static extern  void Pobsclose(ref vconfig_t config) ;

}

关于c# - C# 和非托管 C 库之间的互操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/359541/

相关文章:

c - 先前的堆栈变量

c - 类型转换的段错误

c# - C++/CLI 使用抽象方法从 native C++ 类继承并将其公开给 C#

c# - 返回 JSON 数组中的第二个变量

c# - 不会为动态创建的复选框调用事件处理程序

c# - PHP 到 C# 的转换

c - 在获取 bashrc 时启动应用程序

c# - COM 互操作对象在一个项目中抛出 InvalidCastException,但在其他项目中没有

.net - 将非空终止字符串传递给非托管代码

c# - 如何在 WinForms 中同步 NumericUpDown 和 TrackBar 控件的值?