Shared DLL Data

Started by
8 comments, last by Zomart 21 years, 4 months ago
Hey- I''m looking for a way to have global data shared between more than one DLL. I need to be able to have each DLL communicate with one main DLL to register global data and then each DLL also needs to be able to retrieve any global data from that same main DLL. I can''t seem to think of a way to accomplish this. I''ve tried having all the DLLs load an instance of the main DLL, but that doesn''t work because each time the DLL is loaded a different instance of it occurs, so every DLL would only be able to access data that it registered (it would be pointless). I also looked into hook functions. I haven''t worked with hook functions before, but it seemed like they only handled Windows messages and not application defined functions. If anybody could help or give some suggestions, it would be greatly appreciated. Thanks...
Advertisement
for multiple instances of one dll, a shared data segment has worked for me.
#pragma data_seg(".SHARDATA"static HHOOK ghCWHook  = NULL;static HWND  ghWndTray = NULL;static HWND  ghWnd     = NULL;#pragma data_seg() 

if you use a .def, specify the segment as:
SECTIONS  .SHARDATA  Read Write Shared 

and of course the smiley should be a closing paren.

btw, the static specification is required, as is the initialization of each shared variable.

you can also use a linker comment pragma to specify the segment settings, if you wish.
Yay, something new to learn! I''ve been bored with programming for quite a while and shared data segments I''ve never heard of. I''m already reading tons about them on the web. Thanks a lot man! One question though, how do you retrieve the shared data from another DLL then? Thanks for the help.
sorry, i''ve never needed to do that, so i''m in the dark on that one.
Heh, no problem. You''ve opened me up to a lot that I know will help me solve my problem. Thanks a lot for the help!
You don''t need to get the data out, you can just use the variables as if they were globals. If you do this in one main DLL (so the main DLL has the shared section), you can write simple accessors like HWND GetHandle() to access them from other apps and DLLs.

Member of the Unban Mindwipe Society (UMWS)
A couple of things to note about the shared data segment method that the AP demonstrated. That particular pragma is specific to MSVC. It won''t work with BCC or GCC (etc). Memory mapped files can be used to achieve the same result with non MS compilers. Of course that approach is somewhat more involved.

Second, that method shares data across processes. In effect it makes the data available to other processes running on the system. That''s vital for hooking other processes, but it may not be what you want at al.

If you''re looking to share data between multiple dlls that are all mapped into the same process, EvilBill''s suggestion is the way to go.


"Beautiful maiden," answered Candide, "when a man is in love, is jealous, and has been flogged by the Inquisition, he becomes lost to all reflection."
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
quote:Original post by LessBread
A couple of things to note about the shared data segment method that the AP demonstrated. That particular pragma is specific to MSVC. It won''t work with BCC or GCC (etc).


I think the DLLs he wants to use, are also M$ specific.
So maybe it isn''t really important that it won''t work
with other compilers, for his kind of problem ...
quote:Original post by Beast Master
I think the DLLs he wants to use, are also M$ specific.
So maybe it isn''t really important that it won''t work
with other compilers, for his kind of problem ...


Maybe so, but he doesn''t say which compiler he is using. Perhaps he''s using MSVC, perhaps not. Either way, he might not want to make the data available to external processes. At any rate, I pointed these issues out as noteworthy items. Zomart is free to disregard them.


"Beautiful maiden," answered Candide, "when a man is in love, is jealous, and has been flogged by the Inquisition, he becomes lost to all reflection."
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement