DLLs and Global Variables

Programming, for all ages and all languages.
Post Reply
oscoder
Member
Member
Posts: 59
Joined: Mon Mar 27, 2006 12:00 am
Location: UK

DLLs and Global Variables

Post 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
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: DLLs and Global Variables

Post 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.
User avatar
Combuster
Member
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

Post by Combuster »

But is that shared memory? (hint)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
oscoder
Member
Member
Posts: 59
Joined: Mon Mar 27, 2006 12:00 am
Location: UK

Re: DLLs and Global Variables

Post 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
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: DLLs and Global Variables

Post 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.
gokturkgezer
Posts: 1
Joined: Sat Jul 04, 2009 8:08 pm
Location: Türkiye

Re: DLLs and Global Variables

Post 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.

Code: Select all

 /SECTION:MyDLLSection,RWS
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...
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.
Post Reply