shared memory

   I am trying to create a list in a DLL that is to be shared among all processes which loads/uses this dll. And all these processes would be able to add and delete objects from this list.  I thought one of the easiest way to accomplish this would be to allocate not only the list, but all objects for this list, from shared memory. However, there is nothing in the Win32 SDK documentation which suggest this can be done. Is this the proper way of doing things in Windows?  I guess what I am really asking is:

  Is it possible to have dynamically allocated objects (memory) in a DLL to be accessible to all processes that use this dLL?

I come from UNIX/OS2 environments where this can (easily) be done.  Any help and/or suggestions would be greatly appreicated.  Thanks.


Environment:   NT 4.0
Compiler:      MicroSoft Visual C++ 5.0
chanandrewAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy HuiEngineerCommented:
Yes, it is possible. It is something that I'm currently working on. Note that if you want to share data between different processes using the same DLL, then you need to use something like a memory mapped file to share data. For more information about memory mapped files, search for it in the online books. Don't forget that you also need to use some type of locking mechanism to ensure only one process is reading/writing to the memory mapped file at a time.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chanandrewAuthor Commented:
I read about memory mapped files before my origial posting.
From the online documentation, it doesn't seemed to support
what I want to do.  It will only work if you can memcpy your
object into this memory mapped file so other process can access
it.  However, in my case, I am dynamically allocating C++
objects in which they may contain pointers to other C++ objects.
So memcpy would not work in this case.  Even if it works,
managing the deletion and addtions of these objects would be
a nightmare.  (Remember, my intent was to have a list shared
by many process, each of which can add to / delete from the
list).  Without a mechanism (or APIs) to specify these objects must be allocated from shared memory, how can these C++ objects
be accessible to other processes using the DLL??
0
vinniewCommented:
Have you tried making your objects use the CObject base class, using CObjList to manage them, and putting CObjList in a:

#pragma data_seg("my_list")
CObjList mylist;
#pragma

type statement?

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

chanandrewAuthor Commented:
This will not work for the following reason:

   If we add objects to this CObjList in one process, it will have to dynamically allocate storage to hold the new objects.  Now if the allocated storage isn't from shared memory, how can other processes access it?  So, although the initial list may
very well be acceesible for all processes, the objects added
to it will not be.

I am beginning to wonder if this can be done in the Windows
environment at all.  This is pretty frustrating...
0
fasterCommented:
What kind of object do you need?  If it is just a normal data structure, you should have no problem: Instead of using new/delete, directly use the shared memory to hold your list.  For example, suppose the size of the structure is 100 bytes, and you will need 30 of them at most, then use a ahared memory which is at least 3000 bytes.  You can also put some global variable such as the current size in the shared memory.  Of course, this requires that you predefine the maximum size of the list, but don't worry about that, you can use memory mapped file, in this way you can reserve a large enough address space but use only the necessary part.  Refer to the win32 document about memory mapped file for detail.


0
Tommy HuiEngineerCommented:
You'll also need to have reference counting on the objects. You can use the placement syntax on new to allocate an object onto a specific memory address. Remember that a new operator does the same as a malloc followed by a call to the constructor. Placement syntax assumes you already have the memory and will initialize the object to that memory address.
0
chanandrewAuthor Commented:
I have thought about this too, but it is a lot of work and
very messy too.  With some workaround, I guess I can remove
the pointers in my C++ objects that will be added to the list.  However, if all the proceses accessing this DLL can add to and
delete from this list, think of all the mapping management I
need to do.  I will need to keep track of the parts of the file
that are free, and how large these parts are, etc.  If the OS provides a mechanism to manage this, like all the other GOOD OS do, then I wouldn't me in this mess! This is what I am trying
to avoid.  Sigh...

0
kriszCommented:
Have you checked MapFileOfViewEx? It enables you to specify
the base address of the mapping of shared mem. If you specify
the same address, the pointers will be correct.
0
kriszCommented:
Of course i mean MapViewOfFileEx.
0
chanandrewAuthor Commented:
But this still means that I will have to do all the memory management stuffs myself, right?  Even though we may be able to get around the pointers problem.  Is it what I am trying to do is not possible in Windows at all?  If someone can just tell me so, I will not waste anyone's time on this subject.  Thanks.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.