Я хотел бы поделиться статическими/глобальными переменными только между процессом и DLL, вызываемой процессом. Exe и dll находятся в одном и том же адресном пространстве памяти. Я не хочу, чтобы переменная была разделена между другими процессами.
Разработка проблемы:
Скажите, что существует статическая/глобальная переменная x
в a.cpp
. И 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. Сначала я рассмотрел использование pragma data_seg
в Windows. Однако, даже если я правильно настроил общий сегмент данных, foo.exe
и bar.dll
никогда не разделяют x
. Напомним, что bar.dll
загружается в адресное пространство foo.exe
. Однако, если я запускаю другой экземпляр foo.exe
, тогда x
является общим. Но я не хочу, чтобы x
делился различными процессами. Таким образом, с помощью data_seg
не удалось.
Я могу использовать файл с отображением памяти, создав уникальное имя между exe и dll, которое я сейчас пытаюсь сделать.
Два вопроса:
- Почему поведение Linux и Windows отличается? Может ли кто-нибудь объяснить об этом больше?
- Что было бы самым простым способом решить эту проблему в Windows?