在C ++中的多个DLL之间共享变量 [英] Sharing variables between multiple Dlls in C++

查看:401
本文介绍了在C ++中的多个DLL之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在2个C ++ Dll之间共享一个变量(1000s)。我应该怎么做?

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

MyVariables.Dll 包含:

int a = 0;

ModifyMyVariables.Dll 包含:

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


推荐答案

您可以使用几种基本机制(使用extern不会在图像(EXE,DLL ...)之间共享数据

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. 使用导入/导出符号(使用标准的导入地址表)来共享数据-仅指示链接程序,而不指示加载程序! / Export Table)

  2. 使用静态部分包含数据

  3. 使用包含您的数据的动态部分

  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.

这篇关于在C ++中的多个DLL之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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