Hi there, I got some problem when using QPluginLoader.
1) Firstly, I wrote the interface named “tcpPlunginInterface.hâ€. The content:
#ifndef TCPPLUGININTERFACE_H
#define TCPPLUGININTERFACE_H
#include "tcpclient_os.h"
class tcpPluginInterface
{
public:
virtual tcpClient_Os* getPluginInst( VhostInfo hostInfo, CtcpOpt *tcpOpt,
};
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(tcpPluginInterface,"ltcpOSDLL/1.0.0")
QT_END_NAMESPACE
#endif // TCPPLUGININTERFACE_H
#ifndef TCPPLUGININTERFACE_H
#define TCPPLUGININTERFACE_H
#include "tcpclient_os.h"
class tcpPluginInterface
{
public:
virtual tcpClient_Os* getPluginInst( VhostInfo hostInfo, CtcpOpt *tcpOpt,
QObject *parent = 0) = 0;
};
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(tcpPluginInterface,"ltcpOSDLL/1.0.0")
QT_END_NAMESPACE
#endif // TCPPLUGININTERFACE_H
To copy to clipboard, switch view to plain text mode
2) Secondly, I wrote a class called “ltcpOSâ€. The instance of this class contained a function “getPluginInstâ€, which would return a point type.
“ltcpOS.hâ€:
#ifndef LTCPOS_H
#define LTCPOS_H
#include <QObject>
#include <QtPlugin>
#include "tcpPluginInterface.h"
class ltcpOS
: public QObject,tcpPluginInterface
{
Q_OBJECT
Q_INTERFACES(tcpPluginInterface)
public:
ltcpOS();
~ltcpOS();
tcpClient_Os* getPluginInst( VhostInfo hostInfo, CtcpOpt *tcpOpt,
//the “tcpClient_Os†is a self-defined class.
public:
tcpClient_Os *os;
signals:
public slots:
};
#endif // LTCPOS_H
#ifndef LTCPOS_H
#define LTCPOS_H
#include <QObject>
#include <QtPlugin>
#include "tcpPluginInterface.h"
class ltcpOS : public QObject,tcpPluginInterface
{
Q_OBJECT
Q_INTERFACES(tcpPluginInterface)
public:
ltcpOS();
~ltcpOS();
tcpClient_Os* getPluginInst( VhostInfo hostInfo, CtcpOpt *tcpOpt,
QObject *parent = 0);
//the “tcpClient_Os†is a self-defined class.
public:
tcpClient_Os *os;
signals:
public slots:
};
#endif // LTCPOS_H
To copy to clipboard, switch view to plain text mode
“ltcpos.cppâ€:
#include "ltcpos.h"
ltcpOS::ltcpOS()
{
}
ltcpOS::~ltcpOS()
{
os->deleteLater();
}
Q_EXPORT_PLUGIN2(ltcpOS,ltcpOS)
tcpClient_Os
*ltcpOS
::getPluginInst(VhostInfo hostInfo, CtcpOpt
*tcpOpt,
QObject *parent
){
os = new tcpClient_Os(hostInfo, tcpOpt);
return os;
}
#include "ltcpos.h"
ltcpOS::ltcpOS()
{
}
ltcpOS::~ltcpOS()
{
os->deleteLater();
}
Q_EXPORT_PLUGIN2(ltcpOS,ltcpOS)
tcpClient_Os *ltcpOS::getPluginInst(VhostInfo hostInfo, CtcpOpt *tcpOpt, QObject *parent)
{
os = new tcpClient_Os(hostInfo, tcpOpt);
return os;
}
To copy to clipboard, switch view to plain text mode
Then I compiled these file in release mode, and generated a “ltcpOS1.dll†DLL file. So I could load it and use it in other project.
The problem I encountered is, when I use a QPluginLoader to load the DLL, QT doesn’t know what the return value is. This is my code which loads the DLL:
QObject *tcpObj
= dllLoader.
instance();
if(!tcpObj)
{
qDebug() << dllLoader.errorString();
return;
}
tcpPluginInterface *os_inst = qobject_cast<tcpPluginInterface*>(tcpObj);
client = os_inst->getPluginInst(hostInfo, &ProtIOBuf);
client->run();
QPluginLoader dllLoader("E:/QTProject/tcpClient3/ltcpOS1.dll");
QObject *tcpObj = dllLoader.instance();
if(!tcpObj)
{
qDebug() << dllLoader.errorString();
return;
}
tcpPluginInterface *os_inst = qobject_cast<tcpPluginInterface*>(tcpObj);
client = os_inst->getPluginInst(hostInfo, &ProtIOBuf);
client->run();
To copy to clipboard, switch view to plain text mode
I have included the header file such like “tcpClient_os.hâ€, and add “QT += network†to the *.pro file, but I still get the compile errors:
error:undefined reference to `tcpClient_Os::run()'
error:undefined reference to `tcpClient_Os::slot_stop()'
…..etc
But When I add the all the cpp files (such like tcpClient_os.cpp), which contains the all implement part, to the *.pro. It’s OK. I don’t want to add those cpp files because that made my DLL useless. I’m going to be insane about that problem, it bother me more than a week…Please,help.
Bookmarks