CSharedMemory, A Small Class to Share Data Via File Mapping

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:


  • Infrequent review gives you the basic truth on nike which experts state just a few persons know of.

    Posted by icoppyapedcap on 04/25/2013 10:39am

    CnyXqpMdoLiu [url=http://www.nikeyasuijp.com/]ナイキ[/url]WgfKzdPpsWgh [url=http://www.nikeyasuijp.com/nike-air-force1エアフォース1-c-14.html]ナイキ エアフォース[/url]RqfIkdKdwBob [url=http://www.nikeyasuijp.com/nike-air-maxエアマックス-c-12.html]ナイキ エアマックス[/url]QovNnkZtrLrr [url=http://www.nikeyasuijp.com/nike-air-jordanエア-ジョーダン-c-13.html]nike air jordan[/url]IseXumEyrOqp

  • Will CreateFileMapping always create physical storage in memory?

    Posted by Legacy on 10/29/2001 12: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 12: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 12: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 12: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 12: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.

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

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

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