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查看不到依赖函数,但编程较繁琐。

相关文章:

  • 2022-12-23
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
猜你喜欢
  • 2021-05-31
  • 2021-10-28
  • 2021-07-27
  • 2022-02-23
  • 2021-06-22
相关资源
相似解决方案