Results 1 to 2 of 2

Thread: how to export functions to dll.. tutorial

  1. #1
    Join Date
    Jan 2008
    Posts
    33
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Lightbulb how to export functions to dll.. tutorial

    this is a simple program on how to export functions to dll that can be access by other programming language

    Qt Code:
    1. mydll.h
    2.  
    3. #define EXPORT_MYDLL Q_DECL_EXPORT
    4. #ifndef MYDLL_H
    5. #define MYDLL_H
    6.  
    7. extern "C" {
    8. EXPORT_MYDLL void showqt(); //showqt is a function
    9. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. MYDLL.cpp
    2.  
    3.  
    4. #include <QApplication>
    5. #include <QMessageBox>
    6.  
    7. int main ()
    8. {
    9. showqt();
    10. return 0;
    11. }
    12.  
    13. void showqt()
    14. {
    15. char ** argv;
    16. int i=1;
    17. QApplication lib(i,argv); //this part is important to show the message box
    18.  
    19. QMessageBox::warning(NULL,"ACCESS","You have access QT");
    20.  
    21. return;
    22. }
    To copy to clipboard, switch view to plain text mode 

    in case you want to show a ui in calling your dll


    Qt Code:
    1. #include <QApplication>
    2. #include "ui_dialog.h" // i use a ui name dialog
    3.  
    4. #include mydll.h
    5.  
    6. int main ()
    7. {
    8. showqt();
    9. return 0;
    10. }
    11.  
    12. void showqt()
    13. {
    14. char ** argv;
    15. int i=1;
    16. QApplication lib(i,argv); //this part is important to show the ui
    17.  
    18. QDialog myUi;
    19.  
    20. Ui::Dialog ui;
    21. ui.setupUi(&myUi);
    22. myUi.exec();//use this line to show your ui until the user closes the ui
    23.  
    24. return;
    25. }
    To copy to clipboard, switch view to plain text mode 

    don't forget to set your template as "lib".

    note:"the code above can be access by other programming language like in my case delphi by just calling the dll function if properly imported"

    the code above can export an object from qt and access by other..


    if you have comment on how can i improve my code.. your response is highly appreciated

    if you have codes in exporting class with object and how to access it's functions, if you want, include it in this tread

    if you have codes in exporting functions that has variable needed ex. myfunction(QString name,int age) , if you want you can add it in this thread

    cause still im in the process of learning....

  2. #2
    Join Date
    Jan 2008
    Posts
    33
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to export functions to dll.. tutorial

    after reading and doing several testing i found out answers in my questions...

    how to export class functions...

    mydll.h
    Qt Code:
    1. #define MYDLLEXPORT Q_DECL_EXPORT
    2. #ifndef MYDLL_H
    3. #define MYDLL_H
    4.  
    5. #include "ui_your_ui.h // if you have used ui in your class
    6.  
    7.  
    8. class your_class //some codes
    9. {
    10. //some codes
    11. class_function();
    12. }
    13. extern "C" {
    14. MYDLLEXPORT void showqt();
    15. ...
    16. ..
    17. }
    18. #endif
    To copy to clipboard, switch view to plain text mode 

    your_class.cpp
    Qt Code:
    1. #include mydll.h
    2. class::class
    3. {
    4. //some lines.........
    5. }
    6. class::class_fuction()
    7. {
    8. //some lines..........
    9. }
    To copy to clipboard, switch view to plain text mode 

    mydll.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mydll.h"
    3.  
    4. int main()
    5. {
    6.  
    7. }
    8.  
    9. void showqt()
    10. {
    11. char ** argv;
    12. int argc=1;
    13.  
    14. QApplication lib(argc,argv);
    15. your_class accessclass;//this should be under the QApplication or paint event
    16.  
    17. acessclass.class_function(); //to access the class functions
    18.  
    19. return;
    20. }
    To copy to clipboard, switch view to plain text mode 

    the point here is to access the class::function to exported function


    and lastly to access the functions with parameter depends on how you imported the function....

    if you have suggestions on how can i improve my program, you are free to comment..

Similar Threads

  1. Qt/Embedded Installation error during make
    By mahe2310 in forum Installation and Deployment
    Replies: 5
    Last Post: 7th September 2006, 04:05

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
  •  
Qt is a trademark of The Qt Company.