Page 1 of 1
DLLs and Global Variables
Posted: Thu Jul 02, 2009 4:40 am
by oscoder
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
Re: DLLs and Global Variables
Posted: Thu Jul 02, 2009 6:03 am
by ru2aqare
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:
Code: Select all
extern int SomeData;
extern "C" int SomeFunction(int dummy);
And the corresponding code in the DLL:
Code: Select all
int SomeData;
extern "C" int SomeFunction(int dummy) { return 0; }
And finally the definitions file for the linker:
Code: Select all
LIBRARY MyDll;
EXPORTS
MyData DATA;
SomeFunction;
I'm sure there are other, more detailed examples on the web.
Re: DLLs and Global Variables
Posted: Thu Jul 02, 2009 10:41 am
by Combuster
But is that shared memory? (hint)
Re: DLLs and Global Variables
Posted: Thu Jul 02, 2009 10:47 am
by oscoder
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
Actually, data exported from DLLs is
not shared across contexts.
But is that shared memory? (hint)
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!
Thanks!
OScoder
Re: DLLs and Global Variables
Posted: Thu Jul 02, 2009 11:02 am
by ru2aqare
Combuster wrote:But is that shared memory? (hint)
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.
Re: DLLs and Global Variables
Posted: Sat Jul 04, 2009 8:21 pm
by gokturkgezer
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:
Code: Select all
#pragma data_seg("MyDLLSection")
//.... variable definitions
#pragma data_seg()
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...