Archived

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

One DLL shared between multiple EXEs

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do i have a DLL that contains data that can be accessed by several (well, 2 at the moment) EXEs? I''ve declared my DLLs data like this:
  
// Declare these as system-wide globals (shared) //

#pragma data_seg(".Dll")
vector<ProgramEntry>	g_vProgramEntries;
#pragma data_seg()
#pragma comment(linker, "/section:.Dll,rws")
  
but the data seems to stick in only one EXE. Any ideas? Cheers, Steve Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
From my understanding of the data_seg pragma, the contents of the segment must be initialized in order for those contents to be shared across processes. I don''t know if you can do that with a vector. For details regarding data_seg, consult MSDN.

Another mechanism for sharing data across processes that is similiar to using the data_seg pragma is a file mapping (see MapViewOfFile).

Share this post


Link to post
Share on other sites
Ah thanks! File mapping seems to have done the trick.
One question though: Should i call OpenFileMapping() once in DllMain(), or whenever i need to read / write to the data?

Cheers, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Learn COM programming (ATL), it will benefit you.

Share this post


Link to post
Share on other sites
Yeah, thats more or less exaclty what i do.

Another question: Is there anything similar to CreateEvent() but that has system wide events?
If i call CreateEvent() in app 1, and then call SetEvent() in app 2, it fails because the event doesn''t exist in the context of the second app.

I don''t really want to look at COM / ATL, because this is just a simple (well, obviously not simple enough ) project.

Cheers for the help
Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates

Share this post


Link to post
Share on other sites
Part 1:

Rather than call OpenFileMapping from DllMain, use CreateFileMapping. Why? Because the first time the dll is loaded the memory map will be created, the second time the dll is loaded a handle to the shared memory will be returned just the same as if OpenFileMapping was called.

Part 2:

Before calling SetEvent in process 2 be certain to get a handle to the event. In similar fashion to CreateFileMapping, CreateEvent will either create the event or return a handle to an already created event - provided that the event is named and that you have the name of the event. If an event doesn''t work for you, take a look at mutexes or semaphores. File mappings, events, mutexes and semaphores can all be used across processes.

To learn more about these things, look up "inter process communication" at MSDN.

Share this post


Link to post
Share on other sites