// ShareMem.h: interface for the CShareMem class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SHAREMEM_H__DC737B1E_1A7A_427E_8852_A7E98F696F05__INCLUDED_)
#define AFX_SHAREMEM_H__DC737B1E_1A7A_427E_8852_A7E98F696F05__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CShareMem
{
public:
CShareMem();
CShareMem(int cCount,size_t cSize);
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()
{
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";
}
//清除所有共享内存区域的数据
BOOL CShareMem::Clear()
{
try
{
memset((PBYTE)m_lpvData,0,m_cDataSize*m_cDataCount);
return TRUE;
}
catch(...)
{
return FALSE;
}
}
CShareMem::~CShareMem()
{
if (m_fInit) //
{
UnmapViewOfFile(m_lpvData);
CloseHandle(m_hSharedMemoryFile);
}
}
CShareMem::CShareMem(int cCount,size_t cSize)
{
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";
}
//取得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));// 共享内存名称
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);
}
实在不好意思!开始有个地方有点小错误!:)
memcpy(pbBuff,(PBYTE)m_lpvData + (nIndex + 1)*m_cDataSize,nLeft * m_cDataSize);
memset((PBYTE)m_lpvData + (nIndex +1 )*m_cDataSize,0,(m_cDataCount - nIndex) * m_cDataSize);
memcpy((PBYTE)m_lpvData + (nIndex +1 )*m_cDataSize,pbBuff,nLeft *m_cDataSize); 改为:
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); 一不小心就复制了!
现在的是重新修改后的,望大家见谅!!:)这个类可以方便的进行数据添加,删除!!建议大家使用时将它作为CWinApp派生子类的public成员变量,这样就可以方便的在工程的任何地方调用了!希望对大家有用!:)