动态库dll编写经验

发布时间 2023-04-21 11:31:42作者: lvye1221

流程

(1)新建 dll 工程,空白工程即可

(2)编写 接口 代码

(3)添加 def 文件,用这个最省事


def 文件说明:

LIBRARY geos-function
EXPORTS
isPointInPolygon_GF

C# 调用 c++ 编写的动态库

注意 入口函数 的大小写,用 WinDbg 工具查看

[DllImport("geos-function.dll", EntryPoint = "isPointInPolygon_GF", ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern int isPointInPolygon_GF(int n, IntPtr pData);

调用关键代码,注意:参数字节大小,double是8个,同时注意 数组元素的长度:

            IntPtr data = Marshal.AllocHGlobal(8 * arrDataLen);
            Marshal.Copy(arrData, 0, data, arrDataLen);

            var ret = isPointInPolygon_GF(n, data);

            Marshal.FreeHGlobal(data);