What is the scope of global variables inside a C ++ DLL?

Say that a third-party DLL X has a global variable G

I am writing two separate DLLs (like plugins for the application) P1 and P2 , which dynamically load X.

During the application, two of my plugins P1 and P2 are loaded, so both are loaded at the same time.

Does this mean that I have one instance of G or two?

change

The X usage scenario is a 3D rendering engine that has singlet packages wrapping system resources, it is simply not designed to wait for multiple instances in the process - you can run 2 X strong> applications, but the attempt to initialize X twice in one application will be ruined. Plugins use X , but they will be deployed as separate installers, they will not know about each other and will not use the same X .dll file - typical for each plugin they have their own directory for storing the necessary resources and DLLs.

+4
source share
2 answers

The DLL loading procedures presented in the Win32 API must contain only one copy of the DLL loaded per process ( see the "Notes" section here , specific point 3 and the last paragraph before the "Security Notes" section, which discusses the calculations for each process) . I assume that you are simply calling LoadLibrary or the like, in which case you have one instance of G.

It seems like you can get around this by having multiple copies of the DLLs in question in different places ( as indicated in this question) and loading each of them, in which case you could get multiple copies of all the data in each DLL ( including G).

+3
source

The answer is that in each process there is one instance of the global variable. Everything Explained on MSDN: Dynamic Link Library Data

0
source

Source: https://habr.com/ru/post/1338564/


All Articles