There was a problem with such a plan: there is an application that works without any gui at all, it was decided that this very user interface should be added to it. I try to do this by creating an interface, and a gooch class written in qt and implementing this interface to be crammed into a dll. But here is the most interesting .... and how to run it? After all, if you execute QApplication :: exec (), it will spin endlessly in it and not allow the rest of the code to execute, and if you do not call exec (), then the changes will not be displayed in the gui window. How to be?
interface header
struct MyInterface { virtual int Add(int a, int b) = 0; virtual void Print() = 0; };
extern "C" bool __declspec(dllexport) GetMyInterface(MyInterface** pMyInterface); typedef bool(*GETINTF)(MyInterface** pMyInterface); extern "C" bool __declspec(dllexport) FreeMyInterface(MyInterface** pMyInterface); typedef bool(*FREEINTF)(MyInterface** pMyInterface); implementation of object creation functions
bool GetMyInterface(MyInterface** pMyInterface) { if (!*pMyInterface) { *pMyInterface = new Gui(0); return true; } return false; } bool FreeMyInterface(MyInterface** pMyInterface) { if (!*pMyInterface) { return false; } delete *pMyInterface; pMyInterface = 0; app.exit(); return true; } Gui Head
class Gui : public QObject, public MyInterface { Q_OBJECT public: Gui(QObject *parent); ~Gui(); public slots: int Add(int a, int b); void Print(); private: int argc; char** argv; //QApplication app; QTextEdit* textEdit; }; gui implementation
Gui::Gui(QObject *parent) : QObject(parent), argc(0), argv(0)//, app(argc, argv) { this->textEdit = new QTextEdit; this->textEdit->show(); this->textEdit->append("I'm QT and I'm alive"); //app.exec(); } int Gui::Add(int a, int b) { this->textEdit->append("You check Add function"); return a + b; } void Gui::Print() { this->textEdit->append("I'm QT and I can print everything"); } here I am trying to connect and use this whole thing
int main() { HINSTANCE hDll = LoadLibrary("Library.dll"); MyInterface* myInterface = 0; if (hDll) { std::cout << "Dll load - successful" << std::endl; GETINTF GetMyInterface; GetMyInterface = (GETINTF)GetProcAddress(hDll, "GetMyInterface"); if (!GetMyInterface) { std::cout << "Load F GetMyInterface - fail" << std::endl; FreeLibrary(hDll); _getch(); return 0; } FREEINTF FreeMyInterface; FreeMyInterface = (FREEINTF)GetProcAddress(hDll, "FreeMyInterface"); if (!FreeMyInterface) { std::cout << "Load F FreeMyInterface - fail" << std::endl; FreeLibrary(hDll); _getch(); return 0; } GetMyInterface(&myInterface); std::cout << "My interface print - " << myInterface->Add(10, 5)<<std::endl; myInterface->Print(); _getch(); FreeMyInterface(&myInterface); std::cout << Add(2, 3) << std::endl; Print("a"); PrintWSC("a"); } else { std::cout << "Not load dll" << std::endl; } _getch(); return 0; }
QCoreApplication::processEvents- ixSci