LarissaGodzilla LarissaGodzilla - 14 days ago 5
C++ Question

QLibrary resolve returns false

I´m trying to include classfiles dynamically right now and chose to do so by loading the .dll into a QLibrary. The problem I´m having now is, that when I try to call the resolve()-method it returns 0.

EDIT:
In the meantime the problem has been solved and I decided to edit the code, so others can see how it works:

This is the .dll´s header file:

#ifndef DIVFIXTURE_H
#define DIVFIXTURE_H

#include<QObject>
#include<QVariant>

class __declspec(dllexport) DivFixture : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE DivFixture();
Q_INVOKABLE void setNumerator(QVariant num);
Q_INVOKABLE void setDenominator(QVariant denom);
Q_INVOKABLE QVariant quotient();

private:
double numerator, denominator;
};

#endif


this is the dll´s .cpp-file:

#include "testfixture.h"

DivFixture::DivFixture(){}

void DivFixture::setNumerator(QVariant num)
{
numerator=num.toDouble();
}


void DivFixture::setDenominator(QVariant denom)
{
denominator=denom.toDouble();
}


QVariant DivFixture::quotient()
{
QVariant ret;
ret=numerator/denominator;
return ret;
}

//non-class function to return pointer to class
extern "C" __declspec(dllexport) DivFixture* create()
{
return new DivFixture();
}


And this is how I load my class:

currentFixture.setFileName("C:\\somepath\\testFixture.dll");
if(currentFixture.load());
{
typedef QObject* (*getCurrentFixture)();
getCurrentFixture fixture=(getCurrentFixture)currentFixture.resolve("create");
if (fixture)
{
Fixture=fixture();
}
}

Answer

You need to export your class using __declspec(dllexport)

class __declspec(dllexport) DivFixture : public QObject
{