Sharing a global/static variable between a process and DLL

Posted by minjang on Stack Overflow See other posts from Stack Overflow or by minjang
Published on 2011-02-06T06:32:55Z Indexed on 2011/02/06 7:25 UTC
Read the original article Hit count: 194

Filed under:
|
|
|
|

I'd like to share a static/global variable only between a process and a dll that is invoked by the process. The exe and dll are in the same memory address space. I don't want the variable to be shared among other processes.


Elaboration of the problem:

Say that there is a static/global variable x in a.cpp. Both the exe foo.exe and the dll bar.dll have a.cpp, so the variable x is in both images.

Now, foo.exe dynamically loads (or statically) bar.dll. Then, the problem is whether the variable x is shared by the exe and dll, or not.

In Windows, these two guys never share the x: the exe and dll will have a separate copy of x. However, in Linux, the exe and dll do share the variable x.

Unfortunately, I want the behavior of Linux. I first considered using pragma data_seg on Windows. However, even if I correctly setup the shared data segment, foo.exe and bar.dll never shares the x. Recall that bar.dll is loaded into the address space of foo.exe. However, if I run another instance of foo.exe, then x is shared. But, I don't want x to be shared by different processes. So, using data_seg was failed.

I may it use a memory-mapped file by making an unique name between exe and dll, which I'm trying now.


Two questions:

  1. Why the behavior of Linux and Windows is different? Can anyone explain more about this?
  2. What would be most easiest way to solve this problem on Windows?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c