Go Premium for a chance to win a PS4. Enter to Win


shared memory

Posted on 1997-09-10
Medium Priority
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 360 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?

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.


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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

885 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