1.在Visual Studio中创建“动态链接库”程序。
2.将需要的cpp文件添加到项目中。
3.在cpp文件中添加头文件 #include "pch.h"
4.在cpp文件中的函数定义前添加 extern "C" _declspec(dllexport)
5.生成解决方案。
6.在需要使用dll的C#程序中引入名称空间 using System.Runtime.InteropServices;
7.将需要的dll中的函数导入C#程序,具体代码如下:
[DllImport("DllAddress.dll", EntryPoint = "FunctionName", CallingConvention = CallingConvention.Cdecl)]//DllAddress.dll处填写dll地址,FunctionName处填写需要的函数名
public static extern ReturnType FunctionName();
8.调用函数。