代码实现加载dll,动态调用调用dll接口函数

【背景】

         多个模块对外接口都一样,只是内部实现不同,这些模块都被封装成dll,便于管理、开发和维护,调用这些模块的情况随用户操作接口确定调用哪个dll的。

【实现】

dll封装注:

1、dll封装时需要实现从基类继承的虚函数,并导出两个函数供外部调用:a、CreateLidarPlugin函数用于new子类对象供外部使用;b、DestroyLidarPlugin函数用于释放CreateLidarPlugin函数new的对象

             

 

2、dll都必须继承一个抽象类RawLiDARDataReader,其大致实现如下:

          

 

两个函数指针用于获取子类到处的两个函数

 

外部调用dll的方式:

 

  1. 通过LoadLibaryEx函数加载dll库(dll库存放在exe文件所在文件夹下)

std::string pluginPath;  //dll全路径

std::replace(pluginPath.begin(), pluginPath.end(), '/', '\\');//加载dll必须要双斜杠

std::string lidarType = iter->first;

        WCHAR pWpluginPath[1024] = { 0 };

        MultiByteToWideChar(CP_OEMCP, 0, pluginPath.c_str(), strlen(pluginPath.c_str()) + 1, pWpluginPath, 1024);

        HINSTANCE hDll = LoadLibraryEx(pWpluginPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);

if (NULL == hDll) {

//加载dll失败

}

  1. 通过GetProcAdress函数获取dll向外导出的函数
       CreateLiDarPlugin_t* createLidarPlugin = (CreateLiDarPlugin_t*)GetProcAddress(hDll, "CreateLidarPlugin");
       if (NULL == createLidarPlugin) {
           //从插件中获取函数地址:CreateLidarPlugin 失败"(创建对象的函数)
           FreeLibrary(hDll);  //释放句柄
       }
       DestroyLidarPlugin_t* destroyLidarPlugin = (DestroyLidarPlugin_t*)GetProcAddress(hDll, "DestroyLidarPlugin");
       if (NULL == destroyLidarPlugin) {
         // "从插件中获取函数地址:DestroyLidarPlugin 失败"(释放对象的函数)                                     FreeLibrary(hDll);    //释放句柄

        }

  1. 通过上面获取到的函数创建当前处理所用的对象
       RawLiDARDataReader* rawLidarDataReader = createLidarPlugin();
       if (NULL == rawLidarDataReader)
       {
             // 创建激光解析器对象失败
       }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值