通常外部在调用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