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

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

问题描述

我只想在进程和进程调用的 dll 之间共享静态/全局变量.exe 和 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.

问题的详细说明:

假设a.cpp中有一个静态/全局变量x.exe foo.exe 和 dll bar.dll 都有 a.cpp,所以变量 x 是在两张图片中.

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.

现在,foo.exe 动态加载(或静态)bar.dll.那么,问题是变量x是否被exe和dll共享.

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.

在 Windows 中,这两个家伙从不共享 x:exe 和 dll 将具有 x 的单独副本.但是,在 Linux 中,exe 和 dll 共享变量 x.

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.

不幸的是,我想要 Linux 的行为.我首先考虑在 Windows 上使用 pragma data_seg.但是,即使我正确设置了共享数据段,foo.exebar.dll 也不会共享 x.回想一下,bar.dll 被加载到 foo.exe 的地址空间中.但是,如果我运行另一个 foo.exe 实例,则共享 x.但是,我不希望 x 被不同的进程共享.所以,使用 data_seg 失败了.

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.

我可以通过在 exe 和 dll 之间创建唯一名称来使用内存映射文件,我现在正在尝试这样做.

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

两个问题:

  1. 为什么 Linux 和 Windows 的行为不同?有人能解释一下吗?
  2. 在 Windows 上解决此问题的最简单方法是什么?

推荐答案

首先,我发现这篇文章是一篇关于动态链接库的非常有趣且简洁的读物(这篇文章仅针对 Linux,但这些概念肯定也适用于 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.

我认为您想要或正在尝试实现的是跨模块单例"模式.如果你阅读了这个话题的答案,我不知道我怎么可能回答你的比 Ben Voigt 回答那个帖子更好的问题.我之前(实际上有几次)使用他描述的方法实现了一个跨模块的单例,它的效果非常好.

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.

当然,您将无法保持仅将全局变量放在 cpp 文件中的清洁度.您将不得不使用静态指针和一些访问器函数和引用计数.但它可以工作.我不太确定如何避免 foo.exe 和 foo.exe 共享同一个 bar.dll 全局数据实例,我从来没有这样做过,也想不出办法它,对不起.

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天全站免登陆