Results 1 to 7 of 7

Thread: How to create a Simple DLL and use it in QT 4?

  1. #1
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Wink Re: How to create a Simple DLL and use it in QT 4?

    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:
    Qt Code:
    1. //////////////////////////////////////////
    2. ///myclass.h
    3. class MyClass
    4. {
    5. public:
    6. int x;
    7.  
    8. MyClass() {};
    9. int getX(){return x;};
    10. };
    11.  
    12. //////////////////////////////////////////
    13. ///myclass.cpp
    14. #include "myclass.h"
    15.  
    16. ///the exported function
    17. extern "C" __declspec( dllexport ) MyClass* GetClass()
    18. {
    19. MyClass* cl = new MyClass();
    20. return cl;
    21. };
    22. //////////////////////////////////////////
    23. #///qt_lib_try.pro
    24. TEMPLATE = lib
    25. INCLUDEPATH += .
    26. LIBS+= -lqtgui4 #this line needed by QWidgetFactory
    27. # Input
    28. HEADERS += myclass.h
    29. SOURCES += myclass.cpp
    30. CONFIG += warn_on
    31. //////////////////////////////////////////
    32.  
    33. This compiles a DLL without errors.
    34.  
    35. Here is the client:
    36. ///Client.cpp
    37. #include <QCoreApplication>
    38. #include <QLibrary>
    39.  
    40. #include "myclass.h"
    41.  
    42.  
    43. int main(int argc, char *argv[])
    44. {
    45. typedef MyClass* (*pf)();
    46. pf function=(pf)QLibrary::resolve("libqt_lib_try.dll","GetClass");
    47. MyClass* cl= function();///NULL POINTER....
    48. printf("here is our x from shared object x=%d",cl->getX());
    49. }
    To copy to clipboard, switch view to plain text mode 

    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


    Qt Code:
    1. #ifndef SHARED_H
    2. #define SHARED_H
    3.  
    4. #include "Shared_global.h"
    5.  
    6. class __declspec(dllexport) Shared {
    7. public:
    8. Shared();
    9.  
    10. int getX(){return 100;}
    11.  
    12. };
    13.  
    14.  
    15. __declspec(dllexport) Shared* getShared(){return new Shared();}
    16.  
    17.  
    18. #endif // SHARED_H
    19.  
    20. /////////////////////Client///////////////
    21. #include <QCoreApplication>
    22. #include <QLibrary>
    23.  
    24. #include "shared.h"
    25.  
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. QCoreApplication a(argc, argv);
    30. printf("HELLO\n");
    31. printf("HELLO2\n");
    32. typedef Shared* (*pf)();
    33.  
    34.  
    35. QLibrary l("C:\\QT\\qtcreator-2.0.1\\Shared-build-desktop\\debug\\Shared.dll");
    36. Shared* s = (Shared*)l.resolve("getShared");
    37. printf("HELLO3 %p" , s);
    38. printf("here is our x from shared object x=%d",s->getX());
    39. a.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    Hooorayyyyyyyyy!!!!!
    Last edited by high_flyer; 9th February 2011 at 11:21. Reason: code tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to create a Simple DLL and use it in QT 4?

    Do you really need to use QLibrary to do run-time loading of arbitrary libraries or do you just want to use a DLL?

  3. #3
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create a Simple DLL and use it in QT 4?

    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...
    Last edited by venkateshhegde; 9th February 2011 at 01:27.

  4. #4
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create a Simple DLL and use it in QT 4?

    To Simply use a DLL, Don't we just add to LIBS?
    LIBS += myDLL(.dll)

  5. #5
    Join Date
    Feb 2011
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create a Simple DLL and use it in QT 4?

    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

  6. #6
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create a Simple DLL and use it in QT 4?

    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:
    Shared.zip

    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.
    MainDLL.zip



    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.

  7. #7

    Default Re: How to create a Simple DLL and use it in QT 4?

    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...");}

    };

Similar Threads

  1. 3D engine to create simple 3D objects
    By been_1990 in forum General Discussion
    Replies: 11
    Last Post: 15th December 2010, 11:57
  2. PyQt4: Create simple GUI for python program
    By Norchina in forum Newbie
    Replies: 0
    Last Post: 11th June 2010, 14:50
  3. Replies: 4
    Last Post: 1st May 2009, 11:00
  4. What is wrong with this simple example ?
    By igor in forum Qt Programming
    Replies: 10
    Last Post: 16th January 2007, 11:26
  5. Replies: 2
    Last Post: 12th January 2007, 11:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.