1

As described on https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application I would like to create dll (Windows 10) and load it dynamically be using QLibrary as plugin.

In Dependency Walker I see that exports and dll could be loaded. But resolve gives me always 0. I tried several examples and hinds in web, but no success. What do I wrong?

.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

TEMPLATE = lib
HEADERS += eurolite_dmx512_pro_mk2.h \
    Eurolite_DMX512_Pro_MK2_global.h
SOURCES += eurolite_dmx512_pro_mk2.cpp
DEFINES += EUROLITE_DMX512_PRO_MK2_LIBRARY

global.h

#ifndef EUROLITE_DMX512_PRO_MK2_GLOBAL_H
#define EUROLITE_DMX512_PRO_MK2_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(EUROLITE_DMX512_PRO_MK2_LIBRARY)
#  define EUROLITE_DMX512_PRO_MK2 Q_DECL_EXPORT
#else
#  define EUROLITE_DMX512_PRO_MK2 Q_DECL_IMPORT
#endif
#endif // EUROLITE_DMX512_PRO_MK2_GLOBAL_H

.h

#ifndef SHARED_H
#define SHARED_H

#include "Eurolite_DMX512_Pro_MK2_global.h"
#include <QDebug>

class Device
{
public:
    Device();
    void Testdll() {qDebug("OK");}
};

extern "C" EUROLITE_DMX512_PRO_MK2 int getVersion();
extern "C" EUROLITE_DMX512_PRO_MK2 QWidget *createWidget1();

int EUROLITE_DMX512_PRO_MK2 libStart(void)
{
    Device device;
    return 0;
}

#endif // SHARED_H

.cpp

#include <QWidget>
#include "eurolite_dmx512_pro_mk2.h"

Device::Device() {}

int getVersion()
{
    return 3;
}

QWidget *createWidget1()
{
    QWidget *widget = new QWidget();
    widget->resize(100, 100);
    return widget;
}

plugintest.c++

#include "dmxservice.h"

#include <QApplication>
#include <QDebug>
#include <QLibrary>
#include <QMessageBox>
#include <QFile>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    bool dmxLibAktiv = false;
    QLibrary dmxlib;

    QMessageBox msgBox;

    QFile file(QCoreApplication::applicationDirPath()+"/devices/Eurolite_DMX512_Pro_MK2.dll");
    if(file.exists()==true)
    {
        QLibrary dmxlib(QCoreApplication::applicationDirPath()+"/devices/Eurolite_DMX512_Pro_MK2.dll");
        if(dmxlib.load()==true)
        {
            dmxLibAktiv = dmxlib.isLoaded();
            msgBox.setText("DLL loaded!\n");
            msgBox.exec();
        }
        else
        {
            msgBox.setText("DLL not loaded!\n"+dmxlib.errorString());
            msgBox.exec();
        }
    }
    else
    {
        msgBox.setText("DLL not found '"+QCoreApplication::applicationDirPath()+"'!\n");
        msgBox.exec();
    }

    if (dmxLibAktiv) {
        typedef QWidget *(*CreateWidgetFunction)();
        CreateWidgetFunction cwf = CreateWidgetFunction(dmxlib.resolve("createWidget1"));
        if (cwf) {
            QWidget *widget = cwf();
            if (widget)
                widget->show();
        } else {
            qDebug() << "Could not show widget from the loaded library";
        }
    }
    DmxService w;
    w.show();
    return a.exec();
}
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
Adi
  • 50
  • 1
  • 8

1 Answers1

3

You created a local QLibrary dmxlib(...); inside the if (file.exists()) branch. So the outer dmxlib instance is never initialized.

ADDED: Do not re-declare the variable inside the if block, use setFileName() instead:

QLibrary dmxlib;
...
if (file.exists()) {
  dmxlib.setFileName(QCoreApplication::applicationDirPath()+"/devices/Eurolite_DMX512_Pro_MK2.dll");
  if (dmxlib.load())
    ....
}

// ... use dmxlib
Maxim Paperno
  • 4,485
  • 2
  • 18
  • 22