One DLL shared between multiple EXEs

Started by
6 comments, last by Evil Bill 21 years, 7 months ago
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
Member of the Unban Mindwipe Society (UMWS)
Advertisement
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).
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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
Member of the Unban Mindwipe Society (UMWS)
Learn COM programming (ATL), it will benefit you.
Here''s an MSDN article that (i think) does exactly what you want:

Creating Named Shared Memory
daerid@gmail.com
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
Member of the Unban Mindwipe Society (UMWS)
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Cool, thats exactly what i needed to know. Thanks!


Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)

This topic is closed to new replies.

Advertisement