dll项目:
//MyDll2.dll
#include "windows.h"
#include "stdio.h"
int _stdcall add(int a,int b){
return a+b;
}
int _stdcall substract(int a,int b){
return a-b;
}
//////////////////////////////////////////////////////////////////
//MyDll2.def--模块定义文件
LIBRARY MyDll2
EXPORTS
add
substract
Client项目:
//Client.cpp
//...
void CMyDllTestDlg::OnButtonSubstract()
{
/*
CString str;
str.Format("5-3=%d",substract(5,3));
MessageBox(str);
*/
HMODULE hIns = LoadLibrary("MyDll2.dll");
if(!hIns){
MessageBox("动态链接库加载失败");
return ;
}
typedef int (_stdcall * FUN_ADD)(int,int);
FUN_ADD add;
add =(FUN_ADD)GetProcAddress (hIns,"substract");
if(!add){
MessageBox("函数地址获取失败");
return ;
}
CString str;
str.Format("5+3=%d",add(5,3));
MessageBox(str);
FreeLibrary(hIns);
}
//...
dll调用方式:动态加载,隐式链接。
隐式链接也是用LoadLibrary去加载,故动态加载效率高,且用dumpbin -exports *.dll查看不到依赖函数,但编程较繁琐。