Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
互操作&x27;在C#和非托管C库之间进行绑定_C#_C_Interop_Marshalling - Fatal编程技术网

互操作&x27;在C#和非托管C库之间进行绑定

互操作&x27;在C#和非托管C库之间进行绑定,c#,c,interop,marshalling,C#,C,Interop,Marshalling,我在DLL中有一个小的C库,我需要调用它的一些方法 它使用指针和一些结构,但在其他方面非常简单。问题是,我对.NET与非托管世界的互操作不太了解,到目前为止,我的尝试一直遇到内存访问冲突异常(可能是因为我没有完全正确地获取指针) 有谁能给我一些建议(哦,双关语!)告诉我最好的方法是什么 多谢各位 extern vconfig_t *Pobsopen(Ppoly_t ** obstacles, int n_obstacles); extern int Pobspath(vconfig_t *

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

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

更新

这里有一个链接指向一个简单的示例:

您应该查看本MSDN杂志中提供的可以将C代码片段转换为C#p/调用签名的链接,当然还有帖子

为您的代码段运行该工具可提供以下功能:

[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) ;

}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
公共结构vconfig\t{
///int
公共利益;
///int
公共int N;
///点*
公共系统.IntPtr P;
///int*
public System.IntPtr启动;
///int*
public System.IntPtr next;
///int*
public System.IntPtr prev;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
公共结构多边形{
///点*
public System.IntPtr ps;
///int
公共int pn;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
公共结构Pxy\t{
///双重的
公共双x;
///双重的
公共双y;
}
公共部分类NativeMethods{
///返回类型:vconfig\u t*
///障碍:Ppoly\U t**
///n_障碍:int
[System.Runtime.InteropServices.DllImportAttribute(“,EntryPoint=“Pobsopen”)]
公共静态外部系统.IntPtr Pobsopen(参考系统.IntPtr障碍物,int n_障碍物);
///返回类型:int
///配置:vconfig\u t*
///p0:Ppoint_t->Pxy_t
///poly0:int
///p1:Ppoint_t->Pxy_t
///poly1:int
///输出路径:Ppolyline\u t*
[System.Runtime.InteropServices.DllImportAttribute(“,EntryPoint=“Pobspath”)]
公共静态外部内部路径(参考vconfig\u t config、Pxy\u t p0、int poly0、Pxy\u t p1、int poly1、ref Ppoly\u t output\u route);
///返回类型:void
///配置:vconfig\u t*
[System.Runtime.InteropServices.DllImportAttribute(“,EntryPoint=“Pobsclose”)]
公共静态外部无效Pobsclose(参考vconfig\u t config);
}

我写了一个很长很有帮助的答案,当我发布它时,它被丢弃了

要点是:

  • 你可能会发现pinvoke.net网站很有用,尽管它并不总是准确的
  • NET framework源代码是Win32函数的正确p/invoke签名的一个非常有用的存储库,它经常为我自己的p/invoke问题提供灵感
  • Marshal类包含许多有用的函数,其中一些函数可能有助于解释IntPtr的实际操作
  • 您可能还需要注意固定/固定-特别是对于像您的链表结构这样的东西,虽然我不确定它将如何在您的托管应用程序中使用