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
{
public:
  CSharedMemory m_sm;
  MyDataSruct* m_pData;

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

CMyClass::CMyClass(..)
{
  m_sm.Init("MyUniqueMemoryName",sizeof(MyDataSruct));
  m_pMyData = (MyDataSruct*)m_sm.GetData();
  //maybe if it is the first process
  if( ! m_sm.AlreadyExist())
      ZeroMemory(m_pMyData,sizeof(MyDataSruct));

Using synchronisation:

{
  CSharedMemory::Locker lock(m_sm); 
  DoSomethingWith(m_pData);
}
//or wait only 100 milliseconds
if( m_sm.Lock( 100 ) )
{
  DoSomethingWith(m_pData);
  m_sm.Unlock();
}

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

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

Downloads

Download demo project - 13 Kb
The latest versions may be found at:
www.schikos.de


Comments

  • 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

    Reply
  • 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?

    Reply
  • Update at www.schikos.de

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

    Originally posted by: Michael Schikora


    www.schikos.de/MFC.html

    The new code is without the Memory Leak Al discribes.

    Al, Thanks again !

    Schiko

    Reply
  • 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)
    {
    CDialog::DoDataExchange(pDX);
    CString strTemp = m_strName;
    //{{AFX_DATA_MAP(CSharedMemoryDemoDlg)
    DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
    DDX_Text(pDX, IDC_EDIT_SHARED, m_strShared);
    //}}AFX_DATA_MAP
    // 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.

    Thanks...
    Al

    Reply
  • 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

    Reply
  • 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
    resources.

    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.

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

Top White Papers and Webcasts

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds