Dll and shared variable

孤人 提交于 2019-12-25 18:41:46

问题


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?

Thank you.


回答1:


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/en-us/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.

for example:

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

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

now to modify g_mydata (increment by 1):

InterlockedExchangeAdd((PLONG)&g_mydata, 1);


来源:https://stackoverflow.com/questions/36795484/dll-and-shared-variable

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