title: Sharing a global/static variable between a process and DLL
body: <p>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.</p>
  
  <hr>
  
  <p>Elaboration of the problem:</p>
  
  <p>Say that there is a static/global variable <code>x</code> in <code>a.cpp</code>. Both the exe <code>foo.exe</code> and the dll <code>bar.dll</code> have <code>a.cpp</code>, so the variable <code>x</code> is in both images.</p>
  
  <p>Now, <code>foo.exe</code> dynamically loads (or statically) <code>bar.dll</code>. Then, the problem is whether the variable <code>x</code> is shared by the exe and dll, or not.</p>
  
  <p>In Windows, these two guys <em>never</em> share the <code>x</code>: the exe and dll will have a separate copy of <code>x</code>. However, in Linux, the exe and dll do share the variable <code>x</code>. </p>
  
  <p>Unfortunately, I want the behavior of Linux. I first considered using <code>pragma data_seg</code> on Windows. However, even if I correctly setup the shared data segment, <code>foo.exe</code> and <code>bar.dll</code> never shares the <code>x</code>. Recall that <code>bar.dll</code> is loaded into the address space of <code>foo.exe</code>. However, if I run another instance of <code>foo.exe</code>, then <code>x</code> is shared. But, I don't want <code>x</code> to be shared by different processes. So, using <code>data_seg</code> was failed.</p>
  
  <p>I may it use a memory-mapped file by making an unique name between exe and dll, which I'm trying now.</p>
  
  <hr>
  
  <p>Two questions:</p>
  
  <ol>
  <li>Why the behavior of Linux and Windows is different? Can anyone explain more about this?</li>
  <li>What would be most easiest way to solve this problem on Windows?</li>
  </ol>