Solved

sharing data for several DLLs

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

18 Experts available now in Live!

Get 1:1 Help Now