C#与非托管C库之间的互操作

时间:2008-12-11 14:32:22

标签: 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;

3 个答案:

答案 0 :(得分:5)

您应该查看本MSDN杂志tool中提供的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) ;

}

答案 1 :(得分:1)

也许您应该在C ++ / CLI中编写包装器,因为托管代码和非托管代码之间的互操作非常无缝。

<强>更新

以下是一个简单示例的链接:C data structure to mimic C#’s List>?

答案 2 :(得分:1)

我写了一篇很长且很有帮助的答案,StackOverflow在我发布它时丢弃了它。

要点是:

  1. 您可能会发现网站pinvoke.net很有用,但它并不总是准确的。
  2. .NET框架源是Win32函数的正确p / invoke签名的非常有用的存储库,并且经常为我自己的p / invoke问题提供灵感。
  3. Marshal类包含许多有用的功能 - 其中一些可能有助于解释您实际使用IntPtr所做的事情。
  4. 您可能还需要注意固定/固定 - 特别是对于您的链接列表结构,我认为我不确定它将如何在您的托管应用中使用。