如何在C++中连接代码执行过程中的dll文件

How to connect the dll files in the process of code execution in C++?

本文关键字:过程中 执行 dll 文件 代码 连接 C++      更新时间:2023-10-16

有一个用QT(C++)编写的编译程序。在程序文件夹中添加了dll文件,所有的名称都是Lib1.dll、Lib2.dll等。在程序的执行过程中,我如何获得这些库?也就是说,最初程序是在没有的情况下编译的

例如:

int n = 10;
for(int i=0;i<n;i++)
{
       include("lib"+i+".dll");
}

使用QLibrary 解决了问题

 for(int i=0; i<n;i++)
    {            
       QString* pLoadLib =&sDrivers[i]; //запоминаем адрес переменной
       std::cout<<sDrivers[i].toUtf8().constData()<<"n";//выводим в консоль
       QLibrary lib(*pLoadLib); //Разименование адреса - текст пути к ДЛЛ
       if (!lib.load())
          {
             std::cout<<"driver not loadedn";//error
          }
       else
          {   typedef QString (*FuncType) (const QString&);
                    FuncType func = (FuncType)(lib.resolve("FuncName")); //Определяем функцию func как функция OddUpper из dll
                    if (func)
                    {
                       QString test = func("working");
                       qDebug()<<test;
                    }
                    if (!func)
                    {
                        std::cout<<"func not loaded";
                    }
                    lib.unload();
                }
    }