PDA

View Full Version : how to export functions to dll.. tutorial



triperzz
18th February 2008, 08:01
this is a simple program on how to export functions to dll that can be access by other programming language



mydll.h

#define EXPORT_MYDLL Q_DECL_EXPORT
#ifndef MYDLL_H
#define MYDLL_H

extern "C" {
EXPORT_MYDLL void showqt(); //showqt is a function
}





MYDLL.cpp


#include <QApplication>
#include <QMessageBox>

int main ()
{
showqt();
return 0;
}

void showqt()
{
char ** argv;
int i=1;
QApplication lib(i,argv); //this part is important to show the message box

QMessageBox::warning(NULL,"ACCESS","You have access QT");

return;
}


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




#include <QApplication>
#include "ui_dialog.h" // i use a ui name dialog

#include mydll.h

int main ()
{
showqt();
return 0;
}

void showqt()
{
char ** argv;
int i=1;
QApplication lib(i,argv); //this part is important to show the ui

QDialog myUi;

Ui::Dialog ui;
ui.setupUi(&myUi);
myUi.exec();//use this line to show your ui until the user closes the ui

return;
}



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

triperzz
19th February 2008, 03:27
after reading and doing several testing i found out answers in my questions...

how to export class functions...

mydll.h


#define MYDLLEXPORT Q_DECL_EXPORT
#ifndef MYDLL_H
#define MYDLL_H

#include "ui_your_ui.h // if you have used ui in your class


class your_class //some codes
{
//some codes
class_function();
}
extern "C" {
MYDLLEXPORT void showqt();
...
..
}
#endif


your_class.cpp


#include mydll.h
class::class
{
//some lines.........
}
class::class_fuction()
{
//some lines..........
}


mydll.cpp


#include <QApplication>
#include "mydll.h"

int main()
{

}

void showqt()
{
char ** argv;
int argc=1;

QApplication lib(argc,argv);
your_class accessclass;//this should be under the QApplication or paint event

acessclass.class_function(); //to access the class functions

return;
}



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