1)#define MYLIBAPI extern "C" __declspec(dllexport)
__declspec(dllexport)说明该函数为DLL的导出函数,可供应用程序调用。
__declspec(dllimport)为内部函数,只能在DLL内使用。
2)DLL的调用步骤:DLL加载-DLL函数地址获取-DLL释放。
下面是一个调用DLL的例子:
Code
#include "stdafx.h"
#include "windows.h"
typedef int ( * lpAddFun)(int,int);
int main(int argc, char* argv[])
{
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
hDll = LoadLibrary("..\\Debug\\dllTest.dll");
if (hDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(hDll,"add");
if(addFun!=NULL)
{
int result = addFun(2,3);
printf("%d",result);
}
FreeLibrary(hDll);
}
return 0;
}
3)DLL中导出函数的方式有两种,除了1)里面说的__declspec(dllexport)外,另一种方式是采用模块定义(.def)文件声明。