WEB开发网
开发学院软件开发VC 用File Mapping进行通讯 阅读

用File Mapping进行通讯

 2008-09-23 19:27:24 来源:WEB开发网   
核心提示:class CMemMapChannel{public:#define CMemMapChannel_MAX_DATA_SIZE 4096typedef struct _MAP_DATA{ DWORDdwSize; CHAR m_szBuf[CMemMapChannel_MAX_DATA_SIZE];}MAP_DATA

class CMemMapChannel
{
public:
#define CMemMapChannel_MAX_DATA_SIZE  4096
  typedef struct _MAP_DATA
  {
    DWORD   dwSize;
    CHAR    m_szBuf[CMemMapChannel_MAX_DATA_SIZE];
  }MAP_DATA, * PMAP_DATA;
  CMemMapChannel(CHAR *pChannelName = NULL);
  virtual ~CMemMapChannel();
  DWORD Read(VOID *pData, DWORD dwSize);
  DWORD Write(VOID *pData, DWORD dwSize = 0);
  HANDLE    m_MutexLock;
  HANDLE    m_DataMap;
  PMAP_DATA   m_pData;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//   class CMemMapChannel
/////////////////////////////////////////////////////////////////////////////////////////////////////////
CMemMapChannel::CMemMapChannel(CHAR *pChannelName)
{
  CHAR    szMutexName[1024];
  CHAR    szFileMapName[1024];
  if(pChannelName == NULL)
    pChannelName = "NULL";
  sprintf(szMutexName, "GlobalCMemMapChannel_Mutex_%s", pChannelName);
  sprintf(szFileMapName, "GlobalCMemMapChannel_FileMap_%s", pChannelName);
  m_MutexLock = CreateMutexA(NULL, FALSE, szMutexName);
  if(m_MutexLock == NULL)
  {
    assert(FALSE);
  }
  m_DataMap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL,
    PAGE_READWRITE, 0, sizeof(MAP_DATA), szFileMapName);
  if(m_DataMap == NULL)
  {
    assert(FALSE);
  }
  m_pData = (PMAP_DATA)MapViewOfFile(m_DataMap, FILE_MAP_WRITE, 0, 0, sizeof(MAP_DATA));
  m_pData->dwSize = 0;
}
CMemMapChannel::~CMemMapChannel()
{
  if(m_pData)
    UnmapViewOfFile(m_pData);
  if(m_DataMap)
    CloseHandle(m_DataMap);
  if(m_MutexLock)
    CloseHandle(m_MutexLock);
}
DWORD CMemMapChannel::Read(VOID *pData, DWORD dwSize)
{
  DWORD     dwRetVal;
  DWORD     dwReadSize;
  dwRetVal = WaitForSingleObject(m_MutexLock, INFINITE);
  if(dwRetVal != WAIT_OBJECT_0 &&
    dwRetVal != ERROR_WAIT_NO_CHILDREN)
    return -1;
  if(m_pData->dwSize == 0)
  {
    ReleaseMutex(m_MutexLock);
    return 0;
  }
  if(m_pData->dwSize > dwSize)
  {
    ReleaseMutex(m_MutexLock);
    return 0;
  }
  dwReadSize = m_pData->dwSize;
  memcpy(pData, m_pData->m_szBuf, dwReadSize);
  m_pData->dwSize = 0;
  ReleaseMutex(m_MutexLock);
  return dwReadSize;
}
DWORD CMemMapChannel::Write(VOID *pData, DWORD dwSize)
{
  DWORD     dwRetVal;
  DWORD     dwWriteSize;
  dwRetVal = WaitForSingleObject(m_MutexLock, INFINITE);
  if(dwRetVal != WAIT_OBJECT_0 &&
    dwRetVal != ERROR_WAIT_NO_CHILDREN)
    return -1;
  if(dwSize == 0)
    dwSize = (DWORD)strlen((CHAR *)pData)+1;
  if(m_pData->dwSize != 0)
  {
    ReleaseMutex(m_MutexLock);
    return 0;
  }
  if(dwSize > CMemMapChannel_MAX_DATA_SIZE)
  {
    ReleaseMutex(m_MutexLock);
    return 0;
  }
  dwWriteSize = dwSize;
  memcpy(m_pData->m_szBuf, pData, dwSize);
  m_pData->dwSize = dwSize;
  ReleaseMutex(m_MutexLock);
  return dwWriteSize;}

Tags:File Mapping 进行

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接