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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Information is data with context. The era of Big Data has begun demonstrating to information security that there is more that can, and must, be done to identify threats, reduce risk, address fraud, and improve compliance monitoring activities by bringing better context to data and thereby creating information for actionable intelligence. This analyst report sets the stage and provides insights into IT and information security practitioners' perceptions of the impediments to, and the solutions necessary for, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds