Solved

sharing data for several DLLs

Posted on 2002-06-19
5
360 Views
Last Modified: 2012-08-13
Hello!
I have several DLLs and I want them to use the same data. I've created a SingleTon class in which I hold the common data (which are static pointers!). all my DLLs are accessing that SingleTon using some interface methods, that handle all the treatment in the common data pointers. when the DLLs are being loaded, the common data pointers in the SingleTon are being initialized. the problem is: I want the common data pointers to be init only ONCE, no matter how manny DLLs there are, but the current situation is that the common data pointers are being init for each DLL individually. How can I share the same data exactly for all the DLLs?
0
Comment
Question by:osnat
5 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7092311
Try these links

http://codeproject.com/threads/sharedmemipc.asp
http://codeproject.com/threads/sm.asp

This will give the idea of shared memory.

GOOD LUCK
0
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 7092496
>>How can I share the same data exactly for all the DLLs?

As you aren't sharing the singleton over process boundaries, I'd go about it like this:

(sample for a stringstrem singleton that serves as a global logging buffer):

// a singleton...
template<class _T> class __singleton {

private:

    class __instance_ptr {

    public:

        __instance_ptr () : m_ptr ( NULL) {}

        ~__instance_ptr () { delete m_ptr; }

        _T* get () {

             return m_ptr;
           }

        void set ( _T* __p) {

            if ( __p) {

                delete m_ptr;

                m_ptr = __p;
            }
        }

    private:

        _T* m_ptr;
    };

    static __instance_ptr sm_ptr;

    __singleton ();
    __singleton ( const __singleton&);
    __singleton& operator= ( const __singleton&);

public:

    static _T* instance() {

        if ( !sm_ptr.get()) {

            sm_ptr.set ( new _T ());
        }

        return sm_ptr.get();
    }
};

typedef __singleton<stringstream> LogBuf;
// ensures that it is only initialized ONCE
#ifdef __THE_DLL_THAT_HOLDS_THE_SINGLETON LogBuf::__instance_ptr LogBuf::sm_ptr;
extern stringstream* g_pssLogBuf = LogBuf::instance ();
#else
extern stringstream* g_pssLogBuf;
#endif


stringstream*
declspec ( dllexport)
GetSingletonStringstream  () { return g_pssLogBuf;};



0
 
LVL 1

Expert Comment

by:senohp
ID: 7094992
Dear osnat,

You can share your DLL's variables using  memory map file or #pragma  data_seg(".myseg")

Here is some examples

#pragma data_seg (".myseg")
   int i = 0;
   char a[32]n = "hello world";
#pragma data_seg()

For more detail, please refer to :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_How_do_I_share_data_in_my_DLL_with_an_application_or_with_other_DLLs.3f.asp
0
 
LVL 11

Expert Comment

by:griessh
ID: 7262409
Dear osnat

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "jkr"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7332963
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find boost header files using gnu make (MinGW) on Win 7 11 96
C++ to C# code conversion issue 4 111
Socket Programming (Unix) 8 140
Add values of each row in an array 3 63
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question