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

查看:227
本文介绍了在进程和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的行为。我首先考虑在Windows上使用 pragma data_seg 。但是,即使我正确设置了共享数据段, foo.exe bar.dll 从未共享 X 。回想一下, bar.dll 被加载到 foo.exe 的地址空间中。但是,如果我运行另一个实例 foo.exe ,那么 x 是共享的。但是,我不想让 x 被不同进程共享。因此,使用 data_seg 失败。



我可能会使用内存映射文件,通过在exe和dll,我正在尝试。






两个问题:


  1. 为什么Linux和Windows的行为不同?任何人都可以解释这一点吗?

  2. 在Windows上最简单的解决方法是什么?


解决方案

首先,我发现这篇文章是一个非常有趣和简明扼要的动态链接库(该文章仅针对Linux,但这些概念也适用于Windows,您可能会了解到您所看到的不同行为)。特别是静态和动态加载之间的根本区别。



我想你想要或正在尝试实现的是一个跨模块单例模式。如果您阅读了此线程的答案,我不知道我可以如何回答您的问题比Ben Voigt回答那个帖子更好。我已经使用他描述的方法(几次实际)实现了一个跨模块单例,它的作用就像一个魅力。



当然,你不会能够保持刚刚拥有全局变量的清洁程序坐在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天全站免登陆