Hi there!
I'm currently trying to write a dll, that will be used by multiple processes, and it would be really usefull if I could have a couple of global variables in it. That is, I need a set of variables that the dll can access, whatever context it is in, and whichever process calls it. However, I've heard that you can't use global variables in a dll. I've looked on google and msdn, and not found anything. Can anyone enlighten me as to how I might achieve this?
Thanks in advance,
OScoder
DLLs and Global Variables
Re: DLLs and Global Variables
You didn't look hard enough. Data can be exported from DLLs just as functions can be. It's just another symbol that is exported/imported from the viewpoint of the linker/loader.
Code #included by the consumer:
And the corresponding code in the DLL:
And finally the definitions file for the linker:
I'm sure there are other, more detailed examples on the web.
Code #included by the consumer:
Code: Select all
extern int SomeData;
extern "C" int SomeFunction(int dummy);
Code: Select all
int SomeData;
extern "C" int SomeFunction(int dummy) { return 0; }
Code: Select all
LIBRARY MyDll;
EXPORTS
MyData DATA;
SomeFunction;
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: DLLs and Global Variables
But is that shared memory? (hint)
Re: DLLs and Global Variables
Actually, data exported from DLLs is not shared across contexts.Data can be exported from DLLs just as functions can be. It's just another symbol that is exported/imported from the viewpoint of the linker/loader
Not sure who you are hinting at , but typing 'shared memory' into msdn brings up this, which seems to be the answer to my problem!But is that shared memory? (hint)
Thanks!
OScoder
Re: DLLs and Global Variables
It's shared across any threads that run in the context of the process which loads the DLL. Multiple processes means multiple, independent memory regions. I thought that was what the original poster wanted. If (s)he wants a block of memory shared by multiple processes, he should take look on codeproject.com, which has some example implementations of that.Combuster wrote:But is that shared memory? (hint)
-
- Posts: 1
- Joined: Sat Jul 04, 2009 8:08 pm
- Location: Türkiye
Re: DLLs and Global Variables
For just one or two variable, coping with file-mapping is mess. Try declaring your own PE-File section in your DLL. You can do it by that way:
In definition of your variable in source file, put your variable definition between these two lines:
in the end use this linker switch to link your DLL.
in the linker switch above, S stands for Shared. In every mapping of your DLL into any process, your variables those you defined inside data_seg will be reached from the same position....
(!!!! You have to initialize your variables at definition. It is the rule in custom section declerations. If you don't, your variables won't be placed in the section you declared. They will be placed at .bss section )
Good luck...
In definition of your variable in source file, put your variable definition between these two lines:
Code: Select all
#pragma data_seg("MyDLLSection")
//.... variable definitions
#pragma data_seg()
Code: Select all
/SECTION:MyDLLSection,RWS
(!!!! You have to initialize your variables at definition. It is the rule in custom section declerations. If you don't, your variables won't be placed in the section you declared. They will be placed at .bss section )
Good luck...
Last edited by quok on Sat Jul 04, 2009 11:18 pm, edited 1 time in total.
Reason: Replaced color tags with code tags. Please don't use colors; it's against forum rules.
Reason: Replaced color tags with code tags. Please don't use colors; it's against forum rules.