Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
SOLVED: create Libary wich is used by multiple qt programms, that has one instance
I wrote a Class that connects to an SQL Databes and reads and writes information. I built this Class into a dll Libary. Now here's the question: If I use this .dll in multiple qt applications, does this create a new instance of my class on every usage of the dll, or is there just one instance of my class, which is used by all qt applications.
Tanks for your help
It will create a new instance each time an application uses it
How can i Force them to use all the Same imstance? Or make a class property, which is Used by all instances?
You can't, if you need to access some common data you either have to store them somewhere in memory or in a database etc...
But several applications can't share one instance of an object
Yes, each application (more specifically: process) that loads the DLL gets its own instance. That at least applies to the writable "data" section, which is never shared. The read-only "text" (i.e. program code) section may actually be shared to save physical memory, but that won't help to exchange data between instances. In reality, though, Windows will rarely share DLL "text" sections between processes, due to re-allocation - which requires each process to have its own separate copy of the program code.
BUT: If you want to exchange data between all processes using your DLL, your DLL can internally use a QSharedMemory. Probably in conjunction with QSystemSemaphore instances to avoid race conditions when accessing the shared memory area. This works very nice for me to realize IPC.
Thanks for your answers. Okay so i cant have One instance of my class between more processes. Then the singelton Pattern wont work too?
Maybe there is another Solution for my problem. Basicly i want to exess an SQL Database via different processes and i want to use a class which Handels the Access to the Database. So i tryed to Write a dll with a class in it that conects to the Database and gives Access to it via different slots. There should be only one instance of this class, so that i dont have to use multiple connections to the Database from all the instances. I Hope you get an Idea of what i am Planing to Build :). If you have any better solutions for my Problem please Tell them to me.
Well, you could create some kind of "middle-ware" application that handles all the SQL DB access, running as a special process. The other "client" processes could then communicate with the "middle-ware" process via QSharedMemory and QSystemMutex. The logic that is required for a "client" process to send requests to the "middle-ware" process (and to retrieve the result) could be wrapped into a DLL, for easy re-use in multiple (client) applications.
Thanks for the fast answer :). Sounds Good, do you have an exampelcode or is there a Tutorial wich describes this Kind of Solution?
I have implemented IPC like this: