Click here to Skip to main content
Click here to Skip to main content
Go to top

How to share a data segment in a DLL

, 17 Jan 2000
Rate this:
Please Sign up or sign in to vote.
Using #pragma statements to share variables in a DLL
  • Download demo project - 37 Kb
  • <!-- Article Starts -->

    Introduction

    This is a very simple how to for setting up a shared data segment in a DLL. A shared data segment is used when you want to share variable values among several exe's. This is not as clever as a semaphore and not as powerful as a COM 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.

    A little background.

    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.

    What is a shared data segment?

    A data segment is a chunk of main memory that holds the programs variables. It is the thing that gets swapped to the swap file.

    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.

    What makes a data segment shared?

    These three pragma statements override the default data segment behavior and creates a shared data segment. See MyClass.cpp in the DLL.
    // 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")
    

    Summary

    That's it. The download zip is mostly window dressing. The code does demonstrate how to use a 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.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    Share

    About the Author

    Phil McGahan

    United States United States
    No Biography provided

    Comments and Discussions

     
    QuestionXcode PinmemberMember 1067960929-Mar-14 16:59 
    QuestionHow criticalsection works across process ? PinmemberDeveloperRK11-Apr-12 14:28 
    AnswerRe: How criticalsection works across process ? PinmemberMember 418118213-Jul-12 10:19 
    GeneralMy vote of 5 PinmemberRichard Jackett25-Oct-10 12:03 
    QuestionHow to find whether a function is already called by another EXE? Pinmemberrupanu23-Jul-08 21:41 
    AnswerRe: How to find whether a function is already called by another EXE? PinmemberRaja Iqbal Singh22-Sep-08 1:16 
    GeneralRe: How to find whether a function is already called by another EXE? Pinmembergrmanoj13-Aug-09 22:51 
    GeneralShare data between different processes loding same Dll PinmemberAamol M5-Nov-07 22:32 
    GeneralRe: Share data between different processes loding same Dll Pinmemberehaerim28-Jul-10 11:56 
    QuestionHow to share a file across the processes? PinmemberMember #37409627-Feb-07 19:39 
    Generalsharing data between some processes Pinmembergiugiugiugigugi17-Apr-06 0:35 
    Questionit still seems to run only once?? Pinmembergooglex10-Aug-05 16:29 
    GeneralSharing between different DLLs Pinmemberwangyu4298-Sep-04 4:42 
    GeneralRe: Sharing between different DLLs Pinmemberpeterchen28-Jul-05 2:17 
    QuestionCan I share Handle for a serial com port Pinmemberhedonist18-Jul-04 20:48 
    AnswerRe: Can I share Handle for a serial com port PinsussAnonymous23-Aug-04 12:32 
    GeneralRe: Can I share Handle for a serial com port Pinmemberhedonist23-Aug-04 14:56 
    Questiondll???? Pinmemberdnqhung10-Jun-04 1:26 
    QuestionHow to share a data segment in a DLL Pinmemberbkarl200014-May-04 8:31 
    GeneralDlls and disk access Pinmemberllvllatrix3-Mar-04 16:41 
    GeneralRe: Dlls and disk access PinsussAnonymous23-Aug-04 12:34 
    Generalit doesn't work :( Pinmembermone et pock8-Jan-04 21:13 
    GeneralRe: it doesn't work :( PinmemberMcGahanFL9-Jan-04 2:57 
    GeneralDLL blues Pinmembernewbee7818-Nov-03 4:07 
    GeneralDLL blues Pinmembernewbee7818-Nov-03 4:06 
    GeneralCould not get it to compile. PinmemberWREY25-Oct-03 12:05 
    GeneralRe: Could not get it to compile. PinmemberMcGahanFL27-Oct-03 3:05 
    GeneralGot it to work!! PinmemberWREY17-Sep-04 12:49 
    GeneralThe way to store objects in shared segment Pinmemberbackstab10-Sep-03 23:10 
    GeneralIt doesn't work when on Release way Pinmembergoldust11-Jul-03 10:10 
    GeneralThe 3 basic problems people have. PinmemberMcGahanFL14-Jul-03 2:35 
    GeneralSample CRASHS in Win2k Ad server SP3 PinmemberTommy0130-May-03 5:14 
    GeneralSharing data in ATL dll Pinmemberlalithkx28-May-03 13:28 
    Generalany Info about Proxy DLL (Trojan DLL) Pinmemberbfadi5-Mar-03 10:20 
    GeneralA few serious problems Pinmemberlellis15-Feb-03 13:52 
    GeneralNot critical section, rather Mutual-exclusion PinsussDursun Kirkgöze8-Oct-02 23:12 
    GeneralThe sample doesn't work PinmemberDudi Avramov18-Jun-02 21:31 
    GeneralRe: The sample doesn't work PinmemberRantNRave3130-May-03 9:00 
    GeneralRe: The sample doesn't work PinmemberBlueMonk5-Jun-03 23:07 
    GeneralRe: The sample does work if... PinmemberBlueMonk8-Jun-03 19:58 
    GeneralRe: The sample does work if... PinmemberBlueMonk8-Jun-03 19:59 
    GeneralRe: The sample does work if... Pinmemberkaiser39013-Oct-11 20:18 
    The problem was the initialization. As far as I can remember the documentation is very clear that data in a shared data segment must be initialized. A shared data segment uses old capabilities of the link editor and therefore it has this requirement that is not logical.
    QuestionHas anyone tried with Windows XP? PinmemberPine27-May-02 21:30 
    AnswerRe: OOPs :) PinmemberPine27-May-02 21:47 
    QuestionHow to import class from the DLL? PinmemberMarek Hrncir20-May-02 4:46 
    AnswerRe: How to import class from the DLL? PinmemberRantNRave3130-May-03 8:53 
    GeneralHere is a little "gotcha" and a possible work-around .. PinmemberDavid Patrick9-May-02 8:38 
    GeneralRe: Here is a little "gotcha" and a possible work-around .. PinmemberTim Smith9-May-02 8:52 
    GeneralRe: Here is a little "gotcha" and a possible work-around .. PinmemberDavid Patrick9-May-02 10:34 
    GeneralRe: Here is a little "gotcha" and a possible work-around .. PinmemberDavid Patrick13-May-02 6:02 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Mobile
    Web02 | 2.8.140921.1 | Last Updated 18 Jan 2000
    Article Copyright 2000 by Phil McGahan
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid