c++ - 从 DLL 访问类成员(不是 LIB!!!)

标签 c++ qt dll

我有一个dll,甚至有该dll的头文件,但我没有实现,也没有该dll的lib文件。我尝试使用 QLibrary 类加载 dll 并从中获取类实例。 2 小时后我成功检索了该类,但是当我尝试调用该对象上的函数时,我得到无法解析的外部符号,它告诉我 dll 未正确导出。为简单起见,我使用以下来源重新创建了该问题: DLL 项目 (teSTLibrary_global.hpp):

#ifndef TESTLIBRARY_GLOBAL_HPP
#define TESTLIBRARY_GLOBAL_HPP

#include <QtCore/qglobal.h>

#if defined(TESTLIBRARY_LIBRARY)
#  define TESTLIBRARYSHARED_EXPORT Q_DECL_EXPORT
#else
#  define TESTLIBRARYSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // TESTLIBRARY_GLOBAL_HPP

DLL 项目(teSTLibrary.hpp):

#ifndef TESTLIBRARY_HPP
#define TESTLIBRARY_HPP

#include "testlibrary_global.hpp"

#include <QDebug>

class TESTLIBRARYSHARED_EXPORT TestLibrary {
public:
    TestLibrary();
    ~TestLibrary();
    void Test();
};

extern "C" TESTLIBRARYSHARED_EXPORT TestLibrary* getInstance();

#endif // TESTLIBRARY_HPP

DLL 项目(teSTLibrary.cpp):

#include "testlibrary.hpp"


TestLibrary::TestLibrary() {
    qDebug() << "Constructor called!";
}

TestLibrary::~TestLibrary() {
    qDebug() << "Destructor called!";
}

void Test() {
    qDebug() << "Hello from library!";
}


TestLibrary *getInstance() {
    return new TestLibrary();
}

这非常简单,实际上不包含任何花哨的内容。正如你所看到的,我保留了类默认值,因为 QtCreator 没有改变任何东西,除了添加了另一个带有 extern“C” 的函数和在 global.h 中定义的导出之外。这样做的目的是从 dll 本身获取一个对象(因为我只有 .h 和 .dll)。现在对于加载器应用程序,又是肮脏但简单的基本内容:

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

#include "testlibrary.hpp"

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

    QString libPath = QString("C:/Users/johorvat/Documents/QTProjects/build-TestLibrary-Desktop_Qt_5_2_0_MSVC2010_32bit_OpenGL-Debug/debug/TestLibrary.dll");
    QLibrary lib(libPath);
    bool loaded = lib.load();
    QString error = lib.errorString();
    qDebug() << "Loaded: " << loaded;

    typedef TestLibrary* (*Prototype)();
    Prototype Func = (Prototype) lib.resolve("getInstance");
    if (Func) {
        TestLibrary* tl = Func();
        if (tl) {
            qDebug() << "Yey, I gotta clazz!";
        }
    }

    return a.exec();
}

我将头文件添加到项目中,因为无论如何我都有它。我使用 QLibrary 加载 dll 并从中检索 getInstance 方法,通过该方法我可以获得 TestLibrary 类的实例。但是,如果我尝试在 if(tl) { ... } 中调用 TestLibrary 的 Test() 方法,我会收到一条未解析的外部符号错误消息,告诉我它找不到 Test 方法的定义。 我在这里缺少什么?

P.S.:我不会获得 lib 文件,所以让我们关注 dll 加载的问题:)。

问候, 乔伊

最佳答案

既然您在 .cpp 文件中编写了 void Test() { ,而不是 void TestLibrary::Test { 您的函数没有被定义,所以它根本没有被导出。

编辑: 在这样的代码工作正常并在 qDebug 中打印“Hello”之后(dll 应该在调试中编译,我第一次失败了)

QFunctionPointer raw = lib.resolve("?Test@TestLibrary@@QEAAXXZ");
TestPrototype testFunc;
*(QFunctionPointer*) &testFunc = raw;
(tl->*testFunc) ();

装饰后的函数名称不是很好,但我不知道到底能做什么:) 而且不同的编译器会得到不同的损坏名称,因此在这种情况下使用 Qt 无论如何都不会跨平台.

关于c++ - 从 DLL 访问类成员(不是 LIB!!!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21637872/

相关文章:

c# - 从 C# 应用程序调用 C++ DLL

c++ - 如何在运行时改变QSqlQueryModel子类的数据模型?

c++ - 在 Mac 上部署 Qt 应用程序 : This application failed to start because it could not find or load the Qt platform plugin "cocoa" in ""

c++ - 无法在动态链接库中找到入口点 - C++

c++ - QComboBox 设置标题文本而不考虑项目

qt - 如何在QTableView中选择行?

c# - Libsodium-net - 无法加载 DLL 'libsodium.dll

c++ - CBT Hook 仅接收一些事件

c++ - 使用类的属性制作二维数组,更改值时遇到问题

c++ - 这些参数有什么区别