一个进程和DLL之间共享一个全局/静态变量 [英] Sharing a global/static variable between a process and DLL

查看:1267
本文介绍了一个进程和DLL之间共享一个全局/静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想仅一个过程,通过该过程调用的DLL之间共享的静态/全局变量。该EXE和DLL都在同一个内存地址空间。我不想变向其他进程间共享。


问题的论述:

说,没有在 a.cpp 静态/全局变量 X 。无论是exe文件 foo.exe的和DLL bar.dll a.cpp ,所以变量 X 是两个图像。

现在, foo.exe的动态加载(或静态) bar.dll 。然后,问题是是否变量 X 由EXE和DLL,或者不共享。

在Windows中,这两个家伙的从不的共享 X :该exe和dll将有的单独副本X 。然而,在Linux中,EXE和DLL都共享变量 X

不幸的是,我想要的Linux的行为。我第一次使用杂data_seg 在Windows上考虑。然而,即使我正确地设置共享数据段, foo.exe的 bar.dll 从不共享 X 。回想一下, bar.dll 被装入 foo.exe的的地址空间。但是,如果我跑 foo.exe的的另一个实例,那么 X 是共享的。但是,我不希望 X 要由不同的进程共享。因此,使用 data_seg 已失败。

我可以将它通过EXE和DLL之间的唯一的名字,这我现在正试图使用​​内存映射文件。


两个问题:


  1. 为什么Linux和Windows的行为是不同的?谁能解释更多关于此?

  2. 将是最令解决在Windows这个问题最简单的方法?


解决方案

首先,我发现这篇文章是一个非常有趣和动态链接库读取简洁(本文只是具体到Linux,但其概念肯定适用于Windows和,你可能会得到一些见解,以你所看到的不同的行为)。尤其是静态和动态加载的根本区别。

我想你想要什么,或者试图实现是一个跨模块单的格局。如果你读了答案这个线程,我不知道我怎么可能回答你问题比任何福格特奔更好的回答这个职位。我已经实现了使用他介绍的方法之前(实际上是一个几次)跨模块的单,它就像一个魅力。

当然,你将无法留住只具有全局变量在CPP文件坐在那里的清洁卫生。你将不得不使用一个静态指针和一些访问函数和引用计数。但是它可以工作。我不太确定它如何能够避免foo.exe的和foo.exe的共享全局数据的一个bar.dll的同一个实例,我从来没有这样做,并不能真正想办法做它,对不起。

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?

解决方案

First, I found that this article was a very interesting and a concise read on dynamic link libraries (the article is only specific to Linux, but the concepts surely apply to windows as well and you might get some insight as to the different behaviour you are seeing). Especially the fundamental difference between static and dynamic loading.

I think what you want or are trying to implement is a "cross-module singleton" pattern. If you read the answers to this thread, I don't know how I could possibly answer your question any better than Ben Voigt answered that post. I have implemented a cross-module singleton before (a few times actually) using the method he describes, and it works like a charm.

Of course, you will not be able to retain the cleaniness of just having the global variable sit there in the cpp file. You will have to use a static pointer and some accessor functions and reference counting. But it can work. I'm not so sure how it would be possible to avoid that foo.exe and foo.exe share the same instance of global data one bar.dll, I never had to do that and can't really think of a way to do it, sorry.

这篇关于一个进程和DLL之间共享一个全局/静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆