头文件:
#ifndef __SharedMemory_H__
#define __SharedMemory_H__
class SharedMemory
{
public:
/// 访问模式
enum AccessMode
{
AM_READ = 0, ///< 读
AM_WRITE ///< 写
};
SharedMemory(const std::string& name, std::size_t size, AccessMode mode, const void* addrHint = 0, bool server = true);
SharedMemory(const std::string& fileName, SharedMemory::AccessMode mode, const void* addrHint = 0);
~SharedMemory();
/// 共享内存的起始指针
char* begin() const;
/// 共享内存的尾部指针
char* end() const;
/// 共享内存的名字
std::string name() const;
protected:
void map();
void unmap();
void close();
private:
SharedMemory();
SharedMemory(const SharedMemory&);
SharedMemory& operator = (const SharedMemory&);
std::string m_name;
HANDLE m_memHandle;
HANDLE m_fileHandle;
DWORD m_size;
DWORD m_mode;
char* m_address;
};
inline char* SharedMemory::begin() const
return m_address;
}
inline char* SharedMemory::end() const
return m_address + m_size;
inline std::string SharedMemory::name() const
{
return m_name;
#endif
实现文件
#include "stdafx.h"
#include "SharedMemory.h"
SharedMemory::SharedMemory(const std::string& name,
std::size_t size,
SharedMemory::AccessMode mode,
const void* addrHint,
bool)
: m_name(name)
, m_memHandle(INVALID_HANDLE_VALUE)
, m_fileHandle(INVALID_HANDLE_VALUE)
, m_size(static_cast<DWORD>(size))
, m_mode(PAGE_READONLY)
, m_address(0)
if (mode == SharedMemory::AM_WRITE)
m_mode = PAGE_READWRITE;
}
m_memHandle = ::CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
m_mode,
0,
m_size,
m_name.c_str());
if (!m_memHandle)
//throw SystemException("Cannot create shared memory object", m_name);
map();
SharedMemory::SharedMemory( const std::string& fileName, AccessMode mode, const void* addrHint )
: m_name(fileName)
, m_memHandle(INVALID_HANDLE_VALUE)
, m_fileHandle(INVALID_HANDLE_VALUE)
, m_size(0)
, m_mode(PAGE_READONLY)
, m_address(0)
DWORD shareMode = FILE_SHARE_READ;
DWORD fileMode = GENERIC_READ;
fileMode |= GENERIC_WRITE;
m_fileHandle = ::CreateFile(m_name.c_str(),
fileMode,
shareMode,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (m_fileHandle == INVALID_HANDLE_VALUE)
//throw OpenFileException("Cannot open memory mapped file", m_name);
return;
m_size = ::GetFileSize(m_fileHandle, NULL);
m_memHandle = ::CreateFileMapping(m_fileHandle, NULL, m_mode, 0, 0, NULL);
::CloseHandle(m_fileHandle);
m_fileHandle = INVALID_HANDLE_VALUE;
//throw SystemException("Cannot map file into shared memory", m_name);
SharedMemory::~SharedMemory()
unmap();
close();
void SharedMemory::map()
DWORD access = FILE_MAP_READ;
if (m_mode == PAGE_READWRITE)
access = FILE_MAP_WRITE;
LPVOID addr = ::MapViewOfFile(m_memHandle, access, 0, 0, m_size);
if (!addr)
//throw SystemException("Cannot map shared memory object", m_name);
m_address = static_cast<char*>(addr);
void SharedMemory::unmap()
if (m_address)
::UnmapViewOfFile(m_address);
m_address = 0;
void SharedMemory::close()
if (m_memHandle != INVALID_HANDLE_VALUE)
::CloseHandle(m_memHandle);
m_memHandle = INVALID_HANDLE_VALUE;
if (m_fileHandle != INVALID_HANDLE_VALUE)
示例代码
LPCTSTR szFilePath = TEXT("E:\xxx.zip");
SharedMemory sharedMemory(szFilePath, SharedMemory::AM_READ);
char* pFileBegin = sharedMemory.begin();