一个操作共享内存的容器类!可使用各种数据结构!但是没有写进程间通讯的部分!可方便扩充!
^_^
class CShareMem
{
public:
CShareMem(LPCTSTR lpcszMemName = _T("ShareMemSection"));
CShareMem(int cCount, size_t cSize, LPCTSTR lpcszMemName = _T("ShareMemSection"));
virtual ~CShareMem();
BOOL InitializeMem(int cCount, size_t cSize );
size_t GetDataSize() {return m_cDataSize;};
int GetDataCount() {return m_cDataCount;};
BOOL Clear() ;
BOOL Lock(DWORD dwMilliSeconds = INFINITE);
BOOL Unlock();
LPVOID GetData(int nIndex = 0);
BOOL GetData(int nIndex, LPVOID lpvData, size_t cSize);
BOOL AddData(LPVOID lpvData, size_t cSize);
BOOL SetData(int nIndex, LPVOID lpvData, size_t cSize);
BOOL Delete(int nIndex );
protected:
LPVOID m_lpvData; //数据指针
size_t m_cDataSize; //数据元素大小(BYTE)
int m_cDataCount; //数据元素个数
BOOL m_fInit; //是否初始化成功
HANDLE m_hMutant; //互斥量
HANDLE m_hSharedMemoryFile; //共享内存核心文件句柄
CString m_strMutantName; //互斥量名字
CString m_strShareMemName; //共享内存区域名字
BOOL m_fExist ; //是否已存在
};
#endif // !defined// ShareMem.cpp: implementation of the CShareMem class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ShareMem.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CShareMem::CShareMem(LPCTSTR lpcszMemName)
{
m_cDataCount = 0;
m_cDataSize = 0;
m_fInit = FALSE;
m_fExist = FALSE;
m_lpvData = NULL;
m_hSharedMemoryFile = NULL;
//m_strMutantName = L"ShareMemMutant";
//m_strShareMemName = L"ShareMemSection";
m_strShareMemName = lpcszMemName;
m_strMutantName = lpcszMemName + _T("Mutant");
}
CShareMem::CShareMem(int cCount, size_t cSize, LPCTSTR lpcszMemName)
{
m_cDataCount = cCount;
m_cDataSize = cSize;
m_fInit = FALSE;
m_lpvData = NULL;
m_hSharedMemoryFile = NULL;
m_fExist = FALSE;
//m_strMutantName = L"ShareMemMutant";
//m_strShareMemName = L"ShareMemSection";
m_strShareMemName = lpcszMemName;
m_strMutantName = lpcszMemName + _T("Mutant");
}
CShareMem::~CShareMem()
{
if (m_fInit) //
{
UnmapViewOfFile(m_lpvData);
CloseHandle(m_hSharedMemoryFile);
}
}
//
// 清除所有共享内存区域的数据
//
BOOL CShareMem::Clear()
{
try
{
memset((PBYTE)m_lpvData, 0, m_cDataSize * m_cDataCount);
return TRUE;
}
catch(...)
{
return FALSE;
}
}
//
// 取得nIndex位置上的数据指针
//
LPVOID CShareMem::GetData(int nIndex )
{
if (m_fInit)
{
if (nIndex < 0 || nIndex >= m_cDataCount)
return NULL;
return (PBYTE)m_lpvData + nIndex * m_cDataSize;
}
else
{
return NULL;
}
}
//
// 取得nIndex位置上的数据
//
BOOL CShareMem::GetData(int nIndex,LPVOID lpvData,size_t cSize)
{
if (m_fInit)
{
if (nIndex < 0 || nIndex >= m_cDataCount)
return FALSE;
if (cSize != m_cDataSize )
{
return FALSE;
}
memcpy(lpvData,(PBYTE)m_lpvData + nIndex * m_cDataSize,cSize);
return TRUE;
}
else
{
return FALSE;
}
}
//
// 删除 nIndex位置的数据
//
BOOL CShareMem::Delete(int nIndex)
{
if (m_fInit)
{
if (nIndex < 0 || nIndex >= m_cDataCount)
return FALSE;
int nLeft = m_cDataCount - nIndex - 1;
PBYTE pbBuff = new BYTE[nLeft * m_cDataSize];
//删除头
if (0== nIndex )
{
memcpy(pbBuff,(PBYTE)m_lpvData + m_cDataSize ,(m_cDataCount -1)*m_cDataSize);
memset(m_lpvData,0,m_cDataCount * m_cDataSize);
memcpy(m_lpvData,pbBuff,(m_cDataCount -1)*m_cDataSize);
}
//删除尾
else if ((m_cDataCount -1) == nIndex )
{
memset((PBYTE)m_lpvData + (nIndex *m_cDataSize),0 ,m_cDataSize);
}
else
{
memcpy(pbBuff,(PBYTE)m_lpvData + (nIndex + 1)*m_cDataSize,nLeft * m_cDataSize);
memset((PBYTE)m_lpvData + (nIndex )*m_cDataSize,0,(m_cDataCount - nIndex) * m_cDataSize);
memcpy((PBYTE)m_lpvData + (nIndex )*m_cDataSize,pbBuff,nLeft *m_cDataSize);
}
delete [] pbBuff;
m_cDataCount --;
return TRUE;
}
else
{
return FALSE;
}
}
//
// 添加数据到数据尾
//
BOOL CShareMem::AddData(LPVOID lpvData,size_t cSize)
{
try
{
//
if ((m_cDataSize!= 0 && m_cDataSize != cSize))
{
return FALSE;
}
if (!m_fInit)
{
return FALSE;
}
//
//if (m_cDataCount == 0)
memcpy((PBYTE)m_lpvData + (m_cDataCount )*m_cDataSize, lpvData,cSize);
m_cDataCount ++ ;
return TRUE;
}
catch(CMemoryException *memE)
{
memE->ReportError();
memE->Delete();
return FALSE;
}
catch(...)
{
return FALSE;
}
}
//
// 设置nIndex位置上的数据
//
BOOL CShareMem::SetData(int nIndex,LPVOID lpvData,size_t cSize)
{
if (m_fInit)
{
if (nIndex < 0 || nIndex >= m_cDataCount)
return FALSE;
if (m_cDataSize != cSize)
{
TRACE(_T("数据大小不匹配!!!/n"));
return FALSE;
}
memset((PBYTE)m_lpvData + nIndex * m_cDataSize,0,cSize);
memcpy((PBYTE)m_lpvData + nIndex * m_cDataSize,lpvData,cSize);
return TRUE;
}
else
{
return FALSE;
}
}
//
// 初始化 共享内存和各个变量
//
BOOL CShareMem::InitializeMem(int cCount,size_t cSize)
{
m_hMutant = CreateMutex(NULL,FALSE,m_strMutantName);
m_cDataSize = cSize;
m_cDataCount = cCount;
//创建一内存映射文件,设定共享内存区域
m_hSharedMemoryFile = ::CreateFileMapping(
(HANDLE)0xFFFFFFFF, // 文件句柄
NULL, // 安全属性(WIN95忽略)
PAGE_READWRITE, // 文件的保护属性
0, // 使用页面文件
1<<16, // 共享内存大小
m_strShareMemName.GetBuffer(0));// 共享内存名称
m_strShareMemName.ReleaseBuffer();
if(m_hSharedMemoryFile == NULL)
{
m_fExist = FALSE;
m_fInit = FALSE;
return FALSE;
}
else
{
if(GetLastError() == ERROR_ALREADY_EXISTS)
m_fExist = TRUE;
}
m_lpvData = ::MapViewOfFile(
m_hSharedMemoryFile, // 核心对象的文件HANDLE
FILE_MAP_ALL_ACCESS, // Read/Write
0, // 使用页面文件
0, // 映射整个共享内存区域
0); // 映射整个空间
if(m_lpvData == NULL)
{
m_fInit = FALSE;
CloseHandle(m_hSharedMemoryFile);
return FALSE;
}
else
m_fInit = TRUE;
return TRUE;
}
//
// 互斥锁定共享内存
//
BOOL CShareMem::Lock(DWORD dwMilliSeconds )
{
if( WaitForSingleObject(m_hMutant,dwMilliSeconds) == WAIT_OBJECT_0)
return TRUE;
return FALSE;
}
//
// 互斥解锁共享内存
//
BOOL CShareMem::Unlock()
{
return ReleaseMutex(m_hMutant);
}