Click here to Skip to main content
6,293,171 members and growing! (11,593 online)
Email Password   helpLost your password?
General Programming » DLLs & Assemblies » General     Intermediate

How to share a data segment in a DLL

By Phil McGahan

Using #pragma statements to share variables in a DLL
VC6, MFC, Dev
Posted:17 Jan 2000
Views:191,461
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
34 votes for this article.
Popularity: 6.42 Rating: 4.20 out of 5
3 votes, 14.3%
1

2

3
2 votes, 9.5%
4
16 votes, 76.2%
5
  • Download demo project - 37 Kb
  • 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

    About the Author

    Phil McGahan


    Member

    Location: United States United States

    Other popular DLLs & Assemblies articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 69 (Total in Forum: 69) (Refresh)FirstPrevNext
    GeneralHow to find whether a function is already called by another EXE? Pinmemberrupanu22:41 23 Jul '08  
    GeneralRe: How to find whether a function is already called by another EXE? PinmemberRaja Iqbal Singh2:16 22 Sep '08  
    GeneralShare data between different processes loding same Dll PinmemberAamol M23:32 5 Nov '07  
    GeneralHow to share a file across the processes? Pinmember20:39 7 Feb '07  
    Generalsharing data between some processes Pinmembergiugiugiugigugi1:35 17 Apr '06  
    Generalit still seems to run only once?? Pinmembergooglex17:29 10 Aug '05  
    GeneralSharing between different DLLs Pinmemberwangyu4295:42 8 Sep '04  
    GeneralRe: Sharing between different DLLs Pinsupporterpeterchen3:17 28 Jul '05  
    GeneralCan I share Handle for a serial com port Pinmemberhedonist21:48 18 Jul '04  
    GeneralRe: Can I share Handle for a serial com port PinsussAnonymous13:32 23 Aug '04  
    GeneralRe: Can I share Handle for a serial com port Pinmemberhedonist15:56 23 Aug '04  
    Generaldll???? Pinmemberdnqhung2:26 10 Jun '04  
    GeneralHow to share a data segment in a DLL Pinmemberbkarl20009:31 14 May '04  
    GeneralDlls and disk access Pinmemberllvllatrix17:41 3 Mar '04  
    GeneralRe: Dlls and disk access PinsussAnonymous13:34 23 Aug '04  
    Generalit doesn't work :( Pinmembermone et pock22:13 8 Jan '04  
    GeneralRe: it doesn't work :( PinmemberMcGahanFL3:57 9 Jan '04  
    GeneralDLL blues Pinmembernewbee785:07 18 Nov '03  
    GeneralDLL blues Pinmembernewbee785:06 18 Nov '03  
    GeneralCould not get it to compile. PinmemberWREY13:05 25 Oct '03  
    GeneralRe: Could not get it to compile. PinmemberMcGahanFL4:05 27 Oct '03  
    GeneralGot it to work!! PinmemberWREY13:49 17 Sep '04  
    GeneralThe way to store objects in shared segment Pinmemberbackstab0:10 11 Sep '03  
    GeneralIt doesn't work when on Release way Pinmembergoldust11:10 11 Jul '03  
    GeneralThe 3 basic problems people have. PinmemberMcGahanFL3:35 14 Jul '03  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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
    Web12 | Advertise on the Code Project