Solved

sharing data for several DLLs

Posted on 2002-06-19
5
357 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

778 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