Click here to Skip to main content
Click here to Skip to main content

How to share a data segment in a DLL

By , 17 Jan 2000
 
  • 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
    United States United States
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    QuestionHow to share a file across the processes?memberMember #37409627 Feb '07 - 19:39 
    Is it possible by #pragma data_seg("") like or any other way to share the file ?
     
    and Is there any way to share a data( other than #pragma data_seg(""))
     
    Regards,
    Rammy.
    Generalsharing data between some processesmembergiugiugiugigugi17 Apr '06 - 0:35 
    i want to share data between some processes. my data is structured and have pointers. please help me as soon as posible!
    thanks for help from every one.
    Questionit still seems to run only once??membergooglex10 Aug '05 - 16:29 
    and sencond time,get a kernel error again!
    can anybody explain it?

    GeneralSharing between different DLLsmemberwangyu4298 Sep '04 - 4:42 
    I have a slightly differnet case to deal with. I am running a DLL as main application (since it is loaded by JNI), and the application will load another DLL. Basically, they are different DLLs working in the same process. However, they share some same data structure. The thing I would like to do is to share some of static/global variables, which come from the same LIB file.
     
    Does anyone know how to fulfill that? I tried the data segment but it seems not workingConfused | :confused: . I have put the #pragma statement in my source for LIB file. It compiles and links well, but still, the data are not shared.
     
    Thanks a lot.
    GeneralRe: Sharing between different DLLsmemberpeterchen28 Jul '05 - 2:17 
    You have to export it from one DLL, and import it from another. Maybe you put it in a "micro DLL" just for that.
     

     

    Pandoras Gift #44: Hope. The one that keeps you on suffering.
    aber.. "Wie gesagt, der Scheiss is' Therapie"
    boost your code || Fold With Us! || sighist | doxygen

    QuestionCan I share Handle for a serial com portmemberhedonist18 Jul '04 - 20:48 
    Hi,
     
    I'm writing a dll which access a com port and will send and rec data from this comport to another device.
     
    This Dll will be loaded by 2 different ocx to access the device.
     
    I tried using 2 different application to access this dll concurrently but only the first application is able to send data through the com port.
     
    I've tried to put the my serial Class in the shared data seg as describe here but to no avail.
     
    Could anyone help? Confused | :confused:
     
    Learner
    AnswerRe: Can I share Handle for a serial com portsussAnonymous23 Aug '04 - 12:32 
    See "DuplicateHandle" in the Platform SDK documentation. Handles are not valid across processes.
    GeneralRe: Can I share Handle for a serial com portmemberhedonist23 Aug '04 - 14:56 
    Hi Anonymous,
     
    Thanks for the info. Will visit that to read up.
     
    Smile | :)
    Questiondll????memberdnqhung10 Jun '04 - 1:26 
    I have some problem with inpout32.dll
    in win32 console application project my code is:
     
    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"
    /* ----Prototypes of Inp and Outp--- */
     
    void _stdcall Out32(short PortAddress, short data);
     
    /*--------------------------------*/
    /*
     

    int main()
    {
     
    Out32(888,20);
     
    return 0;
    }
    but in MFC project ,it error (i use the same code ,except
    int out()
    {
     
    Out32(888,20);
     
    return 0;
    }
     
    Linking...
    thulpt1Dlg.obj : error LNK2001: unresolved external symbol "void __stdcall Out32(short,short)" (?Out32@@YGXFF@Z)
    Debug/thulpt1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    thulpt1.exe - 2 error(s), 0 warning(s)
     
    how can i solve it ?
    please help me

     
    dnqhung
    QuestionHow to share a data segment in a DLLmemberbkarl200014 May '04 - 8:31 
    My understanding is that you should not use #pragma data_seg("SHARED"), if pointers are to be shared in the DLL.
     
    See:
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;100634
     
    Since I had to share pointers in my DLL, I used FileMapping with a Mutex to synchronize access. This seems to work, although I am still a bit confused with some of the details.
     
    I would be interested if someone else has used FileMapping/Mutex or other technique to share pointers in a DLL.
     
    Bob

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

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.6.130523.1 | Last Updated 18 Jan 2000
    Article Copyright 2000 by Phil McGahan
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid