More Soma Please…

Developers are in a race with the Universe to create better idiot-proof programs, while the Universe is trying to create better idiots. So far the Universe is winning.

Shared DLL Module State

Posted by Phil Harding on July 19, 2007

Global and static variables in a DLL module are, by default, not shared between processes which load the DLL, as a result, each process has its own instance of a DLL’s global and static variables.

To share DLL global and static variables amongst all processes which load the DLL, you must place the shared variables in a new data segment;

#pragma data_seg(".DATASEGNAME")
int nSomeSharedVariable = 0// initialise variable
#pragma data_seg()

You then need to tell the linker about this data segment and mark it “shared”.

#pragma comment(linker, "/SECTION:.DATASEGNAME,RWS")

Having done this, variable nSomeSharedVariable is shared between all processes which load the DLL.  This obviously means that you must protect such variables from access by multiple threads.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>