Reputation: 31
I'm working on a project in MSVC++ 2012, and I'm trying to split it up into smaller parts. Currently, the dependencies looks something like this:
output.exe --- deploy.dll --- coreext.dll
|
|-- physical.dll --- coreext.dll
|
|-- renderer.dll --- physical.dll --- coreext.dll
| |
| |-- coreext.dll
|
|-- engine.dll ----- physical.dll --- coreext.dll
|
|-- coreext.dll
I have a bit of global data that gets changed in "physical.dll" but it seems that this change doesn't apply to all instances of physical.dll. The value changes at the "deploy.dll" level, but not at the "engine.dll" level. Currently, I'm using implicit linking. There are three more layers of dependencies in the actual output, but I'm just working on these parts here for now.
For the life of me, I cannot seem to figure out how to pass the data around to the proper level either. I put a function in renderer.dll and engine.dll to set the global data, but when going line-by-line with the debugger, it isn't a valid pointer once it goes in - probably some virtual address thing?
I saw this, which gave me the impression this shouldn't be an issue. Then this lead me to this, which I'm reading and trying to wrap my head around it. This is my first time doing this in Windows, and I get the feeling that I'm approaching this sort of design all wrong.
I don't want to do a large load of dumpbin and GetProcAddress()'s as there's thousands of functions to go through, and that would be a pain to maintain (but still automatable I suppose) and the code is nowhere as clean. Besides changing all my DLLs to a collection of OBJ files and linking those - which works, but puts me back where I was - how can I get this working?
Upvotes: 3
Views: 4323
Reputation: 73542
Only processes and threads of a programme have their own life and data.
DLL are just a technical mean to put some identical definitions in a distinct file instead of putting them as usual in the executable. So they don't have their own data. By the way, while there could be several instance of your programme loaded (each having its own independent state and global variables), only one DLL can be loaded once.
In consequence what you show is not multiple instances of the DLL, but multiple dependencies to the same DLL (unless you have DLLs in different versions).
The DLLs content, code and data, is mapped into the address space of the calling process. So a global variable in a DLL are indeed global variables of your programme. The DLL cannot have its own. Eventually, sharing of data between processes using the same DLL is possible but requires creating a shared data segment as explained in this MSDN article.
What is however probable, llooking at your symptoms, is that you define several times a global data (examples : static definitions in a header, or global definitions in a header using an anonymous namespace). In this case, you would think that you always refer to the same global variable, but each .obj, and each .dll would use its own copy.
Upvotes: 1