最近做实验需要用的Geos库做空间拓扑操作,但是前期的代码都是C#写的,而Geos库是使用C++实现的。在网上找了找,可能是本人搜索水平有限,没找到参考样例,所以自己做了些尝试,主要还是C#调用C++动态库的过程。在这里把实验代码贴出来方便自己以后看。水平有限,欢迎各路大神指点。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace GeosUsingCSharp
{
class Program
{
//Geos的C接口要求先调用初始化函数initGEOS()
//initGEOS函数的参数是两个函数指针所以在后面定义了委托(没有测试不用委托行不行)
[DllImport("geos_c.dll", CallingConvention = CallingConvention.Cdecl)]
extern public static void initGEOS(GEOSMessageHandler noticefuction,
GEOSMessageHandler errorfunction);
//GEOSGeomFromWKT从WKT字符串返回指向几何对象的指针
[DllImport("geos_c.dll", CallingConvention = CallingConvention.Cdecl)]
extern public static IntPtr GEOSGeomFromWKT(string wkt);
//GEOSCont