CSharedMemory, A Small Class to Share Data Via File Mapping


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Environment: VC6 SP4, NT4 SP6,W98 SE, W2000 SP1

With this class, only a few lines are needed to communicate with other processes. I use it from storing HWNDs,status flags up to memory resistent fifo queues.

At least one process should keep the memory open. The CSharedMemory as a Member of CMyClass

class CMyClass
  CSharedMemory m_sm;
  MyDataSruct* m_pData;

Initialisation in the constructor of CMyClass with a system wide unique name for this memory block.

  m_pMyData = (MyDataSruct*)m_sm.GetData();
  //maybe if it is the first process
  if( ! m_sm.AlreadyExist())

Using synchronisation:

  CSharedMemory::Locker lock(m_sm); 
//or wait only 100 milliseconds
if( m_sm.Lock( 100 ) )

And because I'm lazy, somtimes I use only this:

CSharedMemory sm("MyUniqueMemoryName",sizeof(MyDataSruct));
((MyDataSruct*)sm.GetData())->m_nMyMember = 123;


Download demo project - 13 Kb
The latest versions may be found at:


  • Will CreateFileMapping always create physical storage in memory?

    Posted by Legacy on 10/29/2001 08:00am

    Originally posted by: soichi

    Will CreateFileMapping always create physical storage for the mapped view on memory? Otherwise, should I expect that there will be any disk access involved? I really don't know anything about the FileMapping, and my feeling is that CreateFileMapping will NEVER access to disk.. but what is the truth?

  • Update at www.schikos.de

    Posted by Legacy on 10/25/2001 07:00am

    Originally posted by: Michael Schikora


    The new code is without the Memory Leak Al discribes.

    Al, Thanks again !


  • Memory and Resource Leak

    Posted by Legacy on 10/24/2001 07:00am

    Originally posted by: Al Dinelt

    First of all, thanks for posting this article on shared
    memory. It has been very useful in helping me understand
    this subject.

    I did find one problem though. I was running the example
    under Windows NT 4.0 and using Bounds Checker. Bounds
    Checker found 3 resource leaks. I brought up the Windows
    Task Manager and noticed the Handles in use would go up
    every time I clicked on the Set or Get button. At the same
    time, the memory available would decrease at the same
    time. It was especially noticeable when I had 5 instances
    of the demo running with the Get Timer turned on.

    I tracked it down to the 'if' statement added to the
    DoDataExchange function. The variable strTemp was always
    set to 'MySharedMemory' and m_strShared would be whatever
    was entered into the Shared Memory edit field. The two
    variables were never equal, so the the shared memory Init
    function was always being called, thereby the resource and
    memory leaks. I commented out the 'if' statement and I no
    longer had the handle and memory problem. (see code
    example below).

    I now have only one resource leak, and that is
    the 'm_hMutex' variable. This could be resolved by keeping
    track of how many times the shared memory is in use. But,
    that would be better discussed in the other section on
    reference counting.

    void CSharedMemoryDemoDlg::DoDataExchange(CDataExchange* pDX)
    CString strTemp = m_strName;
    DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
    DDX_Text(pDX, IDC_EDIT_SHARED, m_strShared);
    // if( m_strShared != strTemp )
    // m_SM.Init(m_strName,SHARED_LENGTH);

    Once again, thanks for this great project on shared memory
    and keep up with the great work.


  • Well..nice try..but not very useful

    Posted by Legacy on 10/22/2001 07:00am

    Originally posted by: Sach

    Well..it was a good try but did not solve much purpose..Cheers-Sach

  • Reference counting

    Posted by Legacy on 10/19/2001 07:00am

    Originally posted by: P. Kramer

    You could add reference counting to the shared memory
    block, an often used mechanism when you have shared

    If you add a reference count to the shared memory block
    you have a bit more control to the life-time of the block.
    As long as one client is using the memory block you can
    keep it alive.

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date