函数部分add.cpp
#include
#include "header.h"
using namespace std;int add(int a,int b)
{
return a + b;
}
头文件header.hpp
#define USB_CAMERA_API _declspec(dllexport)
#include
extern "C"
{
USB_CAMERA_API int add(int a, int b);
}
整个工程如下图所示
编译:点击项目属性选择配置属性为动态库(.dll),然后点击编译,在x64/release文件夹下生成makedll.dll和makedll.lib文件
编写调用程序test.cpp,将生成的makedll.dll和makedll.lib文件复制到test.cpp所在目录
#include
#include
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hInst = LoadLibrary(L"makedll.dll");
if (!hInst)
{
printf("加载MathFuns.dll失败!\n");
}
typedef int(*DllDemoAPIProc)(int a, int b);
DllDemoAPIProc add = (DllDemoAPIProc)::GetProcAddress(hInst, "add");
int result = add(3, 2);
cout << result << endl;
::FreeLibrary(hInst);
system("pause");
return 0;
}