DLL和共享变量 [英] Dll and shared variable

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

问题描述

我有一个Dll(C ++),其中包含一个data_seg,用于在其他程序之间共享变量.它可以工作,并且许多程序都在使用它(超过30个).问题是我将此库添加到了新项目中,但是我尝试访问的变量从未更改其值.我必须重新启动该程序,现在使其与其他程序保持同步,并且可以看到变量的最后一个值.有任何线索吗?

I have a Dll (C++) who contains a data_seg that is used to share variables among others programs. It works and many program is using it (30+). The problem is that I added this library into a new project, but the variable that I try to access, never change its value. I have to restart the program and now its synchronized with the rest of the other programs and I can see the last value of a variable. Any clue?

谢谢.

推荐答案

您提供的信息不多-例如声明变量或修改变量的方式.我认为您已经按照MSDN文档中的说明正确编写了它:

You dont provide much information - like how you declare your variables or how you modify them. I assume you have written it properly as in MSDN documentation:

https://msdn.microsoft.com/zh-CN/library/h90dkhs0%28v=vs.90%29.aspx?f=255&MSPPError=-2147217396

根据您的描述-变量仅在应用程序重新启动后才同步,我只能怀疑您有一些缓存问题.我建议您使变量可变,并使用原子来修改/读取它们.

From your description - that variables are synchronized only after application restart, I can only suspect you have some caching problems. I suggest you make your variables volatile and use atomics to modify/read them.

例如:

#pragma data_seg("Shared")
volatile LONG g_mydata = 0;
#pragma data_seg()

#pragma comment(linker, "/Section:Shared,RWS")

现在修改g_mydata(加1):

now to modify g_mydata (increment by 1):

InterlockedExchangeAdd((PLONG)&g_mydata, 1);

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

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