【问题标题】:Calling functions in a DLL from Qt (c++)从 Qt (c++) 调用 DLL 中的函数
【发布时间】:2017-04-12 12:34:29
【问题描述】:

我想使用从这里下载的 MediaInfo.dll [DLL v0.7.94][1]

[1]:https://mediaarea.net/bg/MediaInfo/Download/Windows。我的问题是如何使用 Qt 框架调用这个 .dll 中的一些函数

#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    if (QLibrary::isLibrary("MediaInfo.dll")) { // C:/MediaInfo.dll
      QLibrary lib("MediaInfo.dll");
      lib.load();
      if (!lib.isLoaded()) {
        qDebug() << lib.errorString();
      }

      if (lib.isLoaded()) {
        qDebug() << "success";
      }
    }

    return a.exec();
}

【问题讨论】:

  • 您是在链接到 DLL 还是在使用 LoadLibrary(windows) / dload (*nix)。
  • @cup 我编辑我的帖子。我不知道如何访问某些功能

标签: c++ qt dll


【解决方案1】:

QLibrary 文档中有一个很好的示例。基本上你必须知道函数名(符号)和它的原型。

#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    if (QLibrary::isLibrary("MediaInfo.dll")) { // C:/MediaInfo.dll
      QLibrary lib("MediaInfo.dll");
      lib.load();
      if (!lib.isLoaded()) {
        qDebug() << lib.errorString();
      }

      if (lib.isLoaded()) {
        qDebug() << "success";

        // Resolves symbol to
        // void the_function_name()
        typedef void (*FunctionPrototype)();
        auto function1 = (FunctionPrototype)lib.resolve("the_function_name");

        // Resolves symbol to
        // int another_function_name(int, const char*)
        typedef int (*AnotherPrototypeExample)(int, const char*);
        auto function2 = (AnotherPrototypeExample)lib.resolve("another_function_name");

        // if null means the symbol was not loaded
        if (function1) function1();
        if (function2) int result = function2(0, "hello world!");
      }
    }

    return a.exec();
}

【讨论】:

    【解决方案2】:

    您需要声明一个函数原型并在 DLL 中获取一个函数的指针。

    QLibrary myLib("mylib");
    typedef void (*MyPrototype)();
    MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
    if (myFunction)
        myFunction();
    

    QLibrary上查看更多信息。

    【讨论】:

      【解决方案3】:

      当有 C/C++ 绑定时,为什么要使用 QLibrary?

      Include file with functions prototypes.
      Example with dynamic call of the DLL.

      有点隐藏,但所有内容都包含在您在问题中提供的链接中的 DLL zip 包中。

      Jérôme,MediaInfo 开发人员

      【讨论】:

        猜你喜欢
        • 2015-12-17
        • 1970-01-01
        • 2020-11-06
        • 2010-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        相关资源
        最近更新 更多