1 new project dllTest,select Win32 Dynamic-Link Library
2 new dll.h,dll.cpp,内容如下:
//dll.h
#ifndef DLL_H
#define DLL_H
extern "C" int _declspec(dllexport)add(int x,int y); //导出动态链接库
extern "C" int _declspec(dllexport)sub(int x,int y);
#endif
//dll.cpp
#include "dll.h"
int add(int x,int y)
{
return x+y;
}
int sub(int x,int y)
{
return x-y;
}
3 编译,生产dllTest.dll
4 调用:new Win32 Console Application project dllCall
(1) typedef int(*lpAddFun)(int,int); //宏定义函数指针类型
(2) HINSTANCE hDll;
lpAddFun AddFun;
lpAddFun SubFun;
(3) hDll=LoadLibrary("dllTest.dll"); //显式链接,加载DLL
(4) AddFun=(lpAddFun)GetProcAddress(hDll,"add"); //获得某个导出函数的指针
(5) AddFun(2,3);
(6) FreeLibrary(hDll); //卸载DLL