Click here to Skip to main content
12,293,601 members (64,616 online)
Click here to Skip to main content
Add your own
alternative version


85 bookmarked

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


    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")


    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.


    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


    About the Author

    Phil McGahan
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralCould not get it to compile. Pin
    WREY25-Oct-03 12:05
    memberWREY25-Oct-03 12:05 
    GeneralRe: Could not get it to compile. Pin
    McGahanFL27-Oct-03 3:05
    memberMcGahanFL27-Oct-03 3:05 
    GeneralGot it to work!! Pin
    WREY17-Sep-04 12:49
    memberWREY17-Sep-04 12:49 
    GeneralThe way to store objects in shared segment Pin
    backstab10-Sep-03 23:10
    memberbackstab10-Sep-03 23:10 
    GeneralIt doesn't work when on Release way Pin
    goldust11-Jul-03 10:10
    membergoldust11-Jul-03 10:10 
    GeneralThe 3 basic problems people have. Pin
    McGahanFL14-Jul-03 2:35
    memberMcGahanFL14-Jul-03 2:35 
    GeneralSample CRASHS in Win2k Ad server SP3 Pin
    Tommy0130-May-03 5:14
    memberTommy0130-May-03 5:14 
    GeneralSharing data in ATL dll Pin
    lalithkx28-May-03 13:28
    memberlalithkx28-May-03 13:28 
    Generalany Info about Proxy DLL (Trojan DLL) Pin
    bfadi5-Mar-03 10:20
    memberbfadi5-Mar-03 10:20 
    GeneralA few serious problems Pin
    lellis15-Feb-03 13:52
    memberlellis15-Feb-03 13:52 
    GeneralNot critical section, rather Mutual-exclusion Pin
    Dursun Kirkgöze8-Oct-02 23:12
    sussDursun Kirkgöze8-Oct-02 23:12 
    GeneralThe sample doesn't work Pin
    Dudi Avramov18-Jun-02 21:31
    memberDudi Avramov18-Jun-02 21:31 
    GeneralRe: The sample doesn't work Pin
    RantNRave3130-May-03 9:00
    memberRantNRave3130-May-03 9:00 
    GeneralRe: The sample doesn't work Pin
    BlueMonk5-Jun-03 23:07
    memberBlueMonk5-Jun-03 23:07 
    GeneralRe: The sample does work if... Pin
    BlueMonk8-Jun-03 19:58
    memberBlueMonk8-Jun-03 19:58 
    GeneralRe: The sample does work if... Pin
    BlueMonk8-Jun-03 19:59
    memberBlueMonk8-Jun-03 19:59 
    GeneralRe: The sample does work if... Pin
    kaiser39013-Oct-11 20:18
    memberkaiser39013-Oct-11 20:18 
    QuestionHas anyone tried with Windows XP? Pin
    Pine27-May-02 21:30
    memberPine27-May-02 21:30 
    AnswerRe: OOPs :) Pin
    Pine27-May-02 21:47
    memberPine27-May-02 21:47 
    QuestionHow to import class from the DLL? Pin
    Marek Hrncir20-May-02 4:46
    memberMarek Hrncir20-May-02 4:46 
    AnswerRe: How to import class from the DLL? Pin
    RantNRave3130-May-03 8:53
    memberRantNRave3130-May-03 8:53 
    GeneralHere is a little "gotcha" and a possible work-around .. Pin
    David Patrick9-May-02 8:38
    memberDavid Patrick9-May-02 8:38 
    GeneralRe: Here is a little "gotcha" and a possible work-around .. Pin
    Tim Smith9-May-02 8:52
    memberTim Smith9-May-02 8:52 
    GeneralRe: Here is a little "gotcha" and a possible work-around .. Pin
    David Patrick9-May-02 10:34
    memberDavid Patrick9-May-02 10:34 
    GeneralRe: Here is a little "gotcha" and a possible work-around .. Pin
    David Patrick13-May-02 6:02
    memberDavid Patrick13-May-02 6:02 
    GeneralRe: Here is a little &quot;gotcha&quot; and a possible work-around .. Pin
    skhall20-Apr-04 7:04
    memberskhall20-Apr-04 7:04 
    GeneralFunction Pointers Pin
    Anonymous9-Nov-01 7:35
    memberAnonymous9-Nov-01 7:35 
    GeneralRe: Function Pointers Pin
    Anonymous9-Nov-01 7:36
    memberAnonymous9-Nov-01 7:36 
    GeneralRe: Function Pointers Pin
    SomeRandomString1-Feb-02 5:29
    memberSomeRandomString1-Feb-02 5:29 
    QuestionHow about DLLS Pin
    Anonymous12-Oct-01 22:59
    memberAnonymous12-Oct-01 22:59 
    AnswerRe: How about DLLS Pin
    tomer dror12-Oct-01 23:28
    membertomer dror12-Oct-01 23:28 
    GeneralShared data must be initialized Pin
    thm11-Apr-01 3:26
    memberthm11-Apr-01 3:26 
    GeneralRe: Shared data must be initialized Pin
    Milind Shingade31-May-01 1:52
    memberMilind Shingade31-May-01 1:52 
    GeneralRe: Shared data must be initialized Pin
    RantNRave3130-May-03 9:02
    memberRantNRave3130-May-03 9:02 
    GeneralRe: Shared data must be initialized Pin
    thm1-Jun-03 21:01
    memberthm1-Jun-03 21:01 
    GeneralHandles Pin
    Jeremy Severson24-Jan-01 10:45
    memberJeremy Severson24-Jan-01 10:45 
    GeneralRe: Handles Pin
    Anonymous11-May-01 13:24
    memberAnonymous11-May-01 13:24 
    GeneralRe: Handles Pin
    hungry2-Jun-03 18:22
    memberhungry2-Jun-03 18:22 
    Phil McGahan2-Oct-00 6:14
    sussPhil McGahan2-Oct-00 6:14 
    GeneralShared Data Segment Pin
    Mark Janveaux7-Sep-00 9:12
    sussMark Janveaux7-Sep-00 9:12 
    QuestionHow to prevent not to share a DLL, when loaded by one executable Pin
    Captain2-May-00 0:59
    sussCaptain2-May-00 0:59 
    QuestionHow to share a non trivial data type Pin
    Shao Lo13-Feb-00 10:55
    sussShao Lo13-Feb-00 10:55 
    AnswerRe: How to share a non trivial data type Pin
    Kevin Bigler23-Feb-00 16:36
    sussKevin Bigler23-Feb-00 16:36 
    GeneralRe: How to share a non trivial data type Pin
    safajoo19-Jul-01 23:51
    membersafajoo19-Jul-01 23:51 
    GeneralRe: How to share a non trivial data type Pin
    safajoo19-Jul-01 23:53
    membersafajoo19-Jul-01 23:53 
    GeneralRe: How to share a non trivial data type Pin
    Alex Galushko6-Nov-01 4:43
    memberAlex Galushko6-Nov-01 4:43 
    AnswerRe: How to share a non trivial data type Pin
    SomeRandomString1-Feb-02 5:22
    memberSomeRandomString1-Feb-02 5:22 
    GeneralYour quite right (in 32 bit) Pin
    Phil5-Feb-00 5:29
    sussPhil5-Feb-00 5:29 
    GeneralRe: Your quite right (in 32 bit) Pin
    Anonymous21-Feb-02 18:30
    memberAnonymous21-Feb-02 18:30 
    QuestionCan not pass pointers. Pin
    Jeff Wyack23-Jan-00 15:30
    sussJeff Wyack23-Jan-00 15:30 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
    Web02 | 2.8.160525.2 | Last Updated 18 Jan 2000
    Article Copyright 2000 by Phil McGahan
    Everything else Copyright © CodeProject, 1999-2016
    Layout: fixed | fluid