1、新建test工程,配置类型选择动态库(.dll)
2、加入工程代码
5、编译test工程,得到test.dll
6、新建demo主工程(exe),我们需要在demo里面引用test.dll
7、增加工程代码如下
2、加入工程代码
int __stdcall add(int a, int b)
{
return a+b;
}
int __stdcall sub(int a, int b)
{
return a-b;
}
3、增加test.def文件,描述如下
LIBRARY test
EXPORTS
add @ 1
sub @ 2
4、连接的命令行中增加 /DEF:test.def
5、编译test工程,得到test.dll
6、新建demo主工程(exe),我们需要在demo里面引用test.dll
7、增加工程代码如下
#include <iostream>
#include <windows.h>
typedef int (__stdcall *pf)(int, int);
int main()
{
HINSTANCE hInstance = LoadLibrary("test.dll");
if (NULL == hInstance)
std::cout << "can not load library." << std::endl;
pf fun = (pf)GetProcAddress(hInstance, MAKEINTRESOURCE(1));
if (NULL == fun)
{
std::cout << "can not find function." << std::endl;
}
std::cout << fun(1, 2) << std::endl;
FreeLibrary(hInstance);
system("pause");
return 0;
}