我在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;
最佳答案
你应该看看这个msdn杂志中给出的tool,它可以将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) ;
}