#### Archived

This topic is now archived and is closed to further replies.

# Shared DLL Data

This topic is 5828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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...

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
sorry, i''ve never needed to do that, so i''m in the dark on that one.

##### Share on other sites
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!

##### Share on other sites
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.

##### Share on other sites
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."

##### Share on other sites
quote:
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 ... #### Share this post ##### Link to post ##### Share on other sites 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."

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 9
• 23
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633383
• Total Posts
3011587
• ### Who's Online (See full list)

There are no registered users currently online

×