3

我在 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;
4

3 回答 3

5

您应该查看这篇 MSDN 杂志文章中提供的工具,该工具可以将 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) ;

}
于 2008-12-11T16:01:30.370 回答
1

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

更新

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

于 2008-12-11T14:37:36.670 回答
1

我写了一个长而有用的答案,当我发布它时,StackOverflow 将其丢弃。

要点是:

  1. 您可能会发现 pinvoke.net 网站很有用,尽管它并不总是准确的。
  2. .NET 框架源代码是一个非常有用的 Win32 函数正确 p/invoke 签名的存储库,并且经常为我自己的 p/invoke 问题提供灵感。
  3. Marshal 类包含许多有用的函数——其中一些可能有助于解释您实际使用 IntPtr 所做的事情。

您可能还需要注意固定/固定 - 特别是对于您的链表结构之类的东西,我认为我不确定它将如何在您的托管应用程序中使用。

于 2008-12-11T16:38:46.520 回答