![]() |
General Programming »
DLLs & Assemblies »
General
Intermediate
How to share a data segment in a DLLBy Phil McGahanUsing #pragma statements to share variables in a DLL |
VC6, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Event_Sink_MAP. However, it has very little overhead and it is very simple
to implement. It is worth while knowing things can be done this easy.
One advantage of using a DLL is that several processes (exe's) can share a code segment. They share the code segment but each process gets it's own data segment. Therefore if five processes use a DLL there is only one code segment and five data segments.
A shared data segment in a DLL is simply one or more variables that are not
unique to a specific process. In many respects this example code is very
similar to a semaphore. If you really think that more than one process at a
time is going to be updating the shared variables, then maybe a semaphore is
the right choice for you. However we will control the update of the shared
variables via critical sections, therefore this technique is just as safe as
a true semaphore. What has more overhead, the ramp up cost of using
CSemaphore all the time or the infrequent spin locking this approach might
incur? The answer depends of what you are doing.
// Global and static member variables that are not shared. ... #pragma data_seg("SHARED") // Begin the shared data segment. // Define simple variables // Integers, char[] arrays and pointers // Do not define classes that require 'deep' copy constructors. #pragma data_seg() // End the shared data segment and default back to // the normal data segment behavior. // This is the most important statement of all // Ideally you can set this in the projects linker tab, but I never could get // that to work. I stumbled across this in a discussion board response from // Todd Jeffreys. This tells the linker to generate the shared data segment. // It does not tell it what variables are shared, the other statements do that, // but it does direct the linker to make provisions for the shared data segment. #pragma comment(linker, "/section:SHARED,RWS")
PostMessage as a simple alternative to COM's message
sink. Furthermore, the code shows how to set up a critical section which is
something you may or may not want. Depending on if you intend to allow
multiple processes to update the shared data segment or just read it.
The code is written in VC 5. First build the DLL TestMemorySpace and then the driver executable Testexe.exe. Start two instances of the test exe, keep them both maximized, and click the button that says 'Read Me First'.
In this simple 'How To' I use two instances of the same exe, you could use X number of different exe's that all share this DLL.
If you learn something from this code - great! If you can improve upon it, I would love to learn from you. I maintain no rights of any kind to it. This code was not plagiarized from any other source.
Good Luck and trust me the code is simpler than the explanation.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 Jan 2000 Editor: Chris Maunder |
Copyright 2000 by Phil McGahan Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |