PDA

View Full Version : How to create a Simple DLL and use it in QT 4?



venkateshhegde
8th February 2011, 02:35
How Do I create a DLL and use it in another application?

I want to create a Class MyClass and getX() as a member function. Then Export a DLL entry MyClass* GetClass();

then Use QLibrary::resolve("DLLname.dll", "GetClass") ; to get instance of MyClass.

How Do I do it?

I am able to create a DLL but the QLibrary call returns NULL.

Here is the source code:

//////////////////////////////////////////
///myclass.h
class MyClass
{
public:
int x;

MyClass() {};
int getX(){return x;};
};

//////////////////////////////////////////
///myclass.cpp
#include "myclass.h"

///the exported function
extern "C" __declspec( dllexport ) MyClass* GetClass()
{
MyClass* cl = new MyClass();
return cl;
};
//////////////////////////////////////////
#///qt_lib_try.pro
TEMPLATE = lib
INCLUDEPATH += .
LIBS+= -lqtgui4 #this line needed by QWidgetFactory
# Input
HEADERS += myclass.h
SOURCES += myclass.cpp
CONFIG += warn_on
//////////////////////////////////////////

This compiles a DLL without errors.

Here is the client:
///Client.cpp
#include <QCoreApplication>
#include <QLibrary>

#include "myclass.h"


int main(int argc, char *argv[])
{
typedef MyClass* (*pf)();
pf function=(pf)QLibrary::resolve("libqt_lib_try.dll","GetClass");
MyClass* cl= function();///NULL POINTER....
printf("here is our x from shared object x=%d",cl->getX());
}


Any help is appreciated.

--
Regards,
ven.

Added after 57 minutes:

Found the answer:

//shared.pro
TARGET = Shared
TEMPLATE = lib

DEFINES += SHARED_LIBRARY

SOURCES += shared.cpp

HEADERS += shared.h\
Shared_global.h



#ifndef SHARED_H
#define SHARED_H

#include "Shared_global.h"

class __declspec(dllexport) Shared {
public:
Shared();

int getX(){return 100;}

};


__declspec(dllexport) Shared* getShared(){return new Shared();}


#endif // SHARED_H

/////////////////////Client///////////////
#include <QCoreApplication>
#include <QLibrary>

#include "shared.h"


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
printf("HELLO\n");
printf("HELLO2\n");
typedef Shared* (*pf)();


QLibrary l("C:\\QT\\qtcreator-2.0.1\\Shared-build-desktop\\debug\\Shared.dll");
Shared* s = (Shared*)l.resolve("getShared");
printf("HELLO3 %p" , s);
printf("here is our x from shared object x=%d",s->getX());
a.exec();
}

Hooorayyyyyyyyy!!!!!

ChrisW67
8th February 2011, 04:15
Do you really need to use QLibrary to do run-time loading of arbitrary libraries or do you just want to use a DLL?

venkateshhegde
9th February 2011, 01:27
Hi Chris,

I need to use QLibrary / LoadLibrary calls to load my DLLs.

I have an interetsing idea that I think may work:

Say, I have a WidgetAVer1.0.dll that contains the factory for my widget. I load it using QLibrary. I also plan to do that with my data model. I use a mediator to bind the two.

if I have to fix defects, I can provide WidgetAVer1.1.dll to the application, and load the new Object that has the fix. The mediator will unbind the GUI and the model AND rebind it with the new GUI.

The above example did not work as it started giving me bad data - probably memory corruption.

Any help in the matter is greatly appreciated.

--
Regards,
ven.

Added after 1 52 minutes:

I am able to get exported "C" functions .. How do I do classes? I want the header file to be in BOTH projects, but the DLL should be via load library...

venkateshhegde
9th February 2011, 11:03
To Simply use a DLL, Don't we just add to LIBS?
LIBS += myDLL(.dll)

Ashutosh2k1
15th February 2011, 06:01
HI
I have coded as u have mentioned in ur file but i am getting undefined refrene to exported function So what could be the possile reason

Thanks

venkateshhegde
16th February 2011, 09:03
Creating and using DLL are two steps. Where are you getting the error?

When You create a DLL, You need the source and header file. When You use it, you need just the header file.

I have attached the entire project for both. Please use VC++ compiler (Its free)

Creation of DLL:
5961

Usage of DLL using Load Library - A Powerful function. You will be able to Hot Swap running code (with good design) at runtime with this.
5962



Note that I have played around with the .H File. In the main app, I have set it to Pure Virtual function so that the linker does not try to find symbol definition while linking.

--
Regards,
Ven.

martynw_swindon
3rd March 2011, 14:10
Hi Ven, thanks for that code, I'm new to QT and it was just what I was looking for.

I wasn't happy with duplicating the header or having that second global header and neither are needed. Remove them from the Main project and simply add the Shared folder in the include path by adding this line to the .pro file

INCLUDEPATH += ../Shared

Then change the one remaining shared.h file to this:

#include <QtCore/qglobal.h>
#include <stdio.h>

#if defined(SHARED_LIBRARY)
# define SHARED_EXT_CLASS Q_DECL_EXPORT
#else
# define SHARED_EXT_CLASS Q_DECL_IMPORT
#endif

class SHARED_EXT_CLASS Shared {
public:

Shared();

virtual int getX();
virtual void doFunc(){printf("doFunc...");}

};