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; } 
  • Be sure to do dll? It would be possible to make a separate GUI application interacting with the console in any way. - asianirish
  • Fun app you have. Try using QCoreApplication::processEvents - ixSci
  • And if you do the opposite - to issue a console application with a loadable library, and in the gui-application you have to pull the necessary functions from there? - Majestio
  • Just to understand how to implement this idea. The fact is that I’m creating a class with gpi inherited from QObject, and I have a pointer to it. But without QApplication, it cannot work, so it’s not enough just to get this pointer in a win32 application, is it necessary to create QApplication somewhere, when to create QApplication in such a case? - MAXIM

1 answer 1

In short, the problem is solved quite simply, in the win32 application, it was necessary to call the function to create Gpi (in which QApplication is created by the way) in a separate thread - that's the whole solution ...