动态加载和静态加载

通常外部在调用dll的时候需要考虑是使用动态加载还是静态加载方式,动态加载对应的可以动态释放,一般多用于程序正常运行但不需要关闭就可以完成更新工作

静态加载的话一般dll不会发生更新,要更换的话,只能停止应用程序

#include "BaseHead.h"
#include <stdio.h>
#include <Windows.h>

typedef IInterFace* (*pFunDevide)();
typedef IInterFaceRst* (*pFunRst)();

int main()
{
	HMODULE hDevices = LoadLibrary(INTER_DLL);
	if(NULL == hDevices)
	{
		printf("Load Devices Lib error\n");
	}

	pFunDevide pDevide = (pFunDevide)GetProcAddress(hDevices,"getObjDevide");
	pFunRst pRst = (pFunRst)GetProcAddress(hDevices,"getObjRst");

	if(NULL == pDevide || NULL == pRst)
	{
		printf("getprocAddress error");
		
		return 0;
	}

	IInterFace *pInterDevice = pDevide();
	IInterFaceRst *pInterRst = pRst();

	pInterDevice->fun1();

	pInterDevice->fun2();

	pInterRst->funrst1();

	pInterRst->funrst2();

	pInterDevice->ReleaseObj();

	pInterRst->ReleaseObj();

<span style="white-space:pre">	</span>FreeLibrary(hhDevice);

	system("pause");

	return 0;
}
在编写过程需要注意连个点
1:getProcAddress的第二个参数要求用c的编译方式不然的话返回至为NULL
2:得到的结果需要用之前定义好的接口函数指针装换,使用完的话需要releaseObj


相对动态加载而言,静态加载的代码编写就方便很多
首先要正确把头文件和dll的路径包含过来
<pre name="code" class="cpp">#include "../InterFaceForm/InterFace.h"

#ifdef _DEBUG
#pragma comment(lib,"../bind/InterFaceForm_d.lib")
#else
#pragma comment(lib,"../bin/InterFaceForm.lib")
#endif


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值