首先打开vs2017
然后添加源文件写入
// MathFuncDll.cpp: 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h" //预编译头
#include "MathFuncDll.h"
#include <stdexcept> //标准的异常类
double __stdcall Add(double a, double b)
{
return a + b;
}
double __stdcall Divide(double a, double b)
{
if (b == 0)
{
throw std::invalid_argument("b 不能是 0!");
}
return a / b;
}
添加头文件
#pragma once
extern "C" _declspec(dllexport) double __stdcall Add(double a, double b);
extern "C" _declspec(dllexport) double __stdcall Divide(double a, double b);
最后添加
写入;
LIBRARY "Dll1"
EXPORTS
Add @ 1
Divide @ 2
最后生成dll.
(2)关于c#调用dll,如果c#调用a.dll,同时a.dll要加载b.dll;
第一种把b.dll放到系统syswow64里,第二种就是把a和b放在同级目录下,系统会自动搜索。