Sharing variables between multiple Dlls in C++

主宰稳场 提交于 2019-12-25 07:54:23

问题


I need to share a variables (1000s) between 2 C++ Dlls. How should I do that?

MyVariables.Dll contains:

int a = 0;

ModifyMyVariables.Dll contains:

extern int a;    
a++; 
// do more stuff with a;

What am i supposed to write in the following files?

myvariables.h
myvariables.cpp
ModifyMyVariables.h
ModifyMyVariables.cpp

回答1:


You can share data between images (EXE, DLL...) using several fundamental mechanisms (using extern does not work to share data - it only instructs the linker and not the loader!)

  1. using import/export symbols (using the standard Import Address Table/Export Table)
  2. using static sections containing your data
  3. using dynamic sections containing your data

In your case, I would use the sections. This works pretty good. You must, of course, take care of the synchronisation when accessing (writing) these data from both sides.



来源:https://stackoverflow.com/questions/11485523/sharing-variables-between-multiple-dlls-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!