Solved

sharing data for several DLLs

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

861 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now