shared memory

chanandrew used Ask the Experts™
   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
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.


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??

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;

type statement?

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.


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...

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.

Tommy HuiEngineer

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.


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...

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.

Of course i mean MapViewOfFileEx.


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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial