1.C++编译Dll文件
创建项目
-
新建项目
-
选择Visual C++
-
Windows桌面
-
动态链接库
编写代码
-
在dllMain文件内输入
-
//返回值为整数型的调用 分步调用 extern "C" __declspec(dllexport) int __stdcall Add(int a, int b); int __stdcall Add(int a, int b) { return a + b; } //返回值为String类型的调用 一次性调用 extern "C" __declspec(dllexport) char* STD(char * text) { return text; } //extern "C" 以C语言标准格式编译dll //__declspec(dllexport) 导出类型 //char* 函数返回类型 //STD() 函数名称 //STD(char * text) text 函数
编译项目
-
选择生成
-
重新生成解决方案
-
右击项目
-
在文件资源管理器中打开文件夹
-
找到对应的Debug目录
-
复制其中的Dll文件即可
2.C#调用dll文件
“`c
[DllImport("调用的DLL目录/dll文件.dll", EntryPoint = "函数名称", CallingConvention =CallingConvention.Cdecl)]
public static extern int Add(int a, int b);
//以上需在类中编写
//以下需在函数中编写
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Add(1,2).ToString());
//MessageBox.Show(Add(1, 2).ToString());
}
“`