shared memory

Posted on 1997-09-10
Last Modified: 2013-12-03
   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
Question by:chanandrew
  • 4
  • 2
  • 2
  • +2
LVL 15

Accepted Solution

Tommy Hui earned 180 total points
ID: 1406462
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.


Author Comment

ID: 1406463
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??

Expert Comment

ID: 1406464
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?

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


Author Comment

ID: 1406465
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...

Expert Comment

ID: 1406466
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.

LVL 15

Expert Comment

by:Tommy Hui
ID: 1406467
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.

Author Comment

ID: 1406468
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...


Expert Comment

ID: 1406469
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.

Expert Comment

ID: 1406470
Of course i mean MapViewOfFileEx.

Author Comment

ID: 1406471
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.


Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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