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!!!!!
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!!!!!