共享内存的使用
2015-06-24 20:15
330 查看
BOOL CShareMemoryImage::InitMap()
{
EnterCriticalSection(&m_cs);
char szBuf[256] = { 0 };
// 获取最小分页大小
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
DWORD dwAllocationGranularity = sysinfo.dwAllocationGranularity;
INT64 dwMemoryFileSize = 1024*1024; //指定内存映射文件大小
// 必须要为dwAllocationGranularity(64K)的整数倍
int left = dwMemoryFileSize % dwAllocationGranularity;
if (left != 0)
{
dwMemoryFileSize += (dwAllocationGranularity - left);
}
DWORD dwError = 0;
// 文件方式创建共享内存
HANDLE hFile = CreateFileA(fileName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
sprintf_s(szBuf, "[CShareMemoryImage]CreateFile failed,GetLastError = %d\n", dwError);
DebugStringA(szBuf);
LeaveCriticalSection(&m_cs);
return FALSE;
}
DebugStringA("[CShareMemoryImage]CreateFile success!\n");
DWORD high = (dwMemoryFileSize >> 32) & 0xFFFFFFFF;// 高32位
DWORD low = dwMemoryFileSize & 0xFFFFFFFF;// 低32位
HANDLE hFileMapping = CreateFileMappingA(
hFile, // system paging file INVALID_HANDLE_VALUE
NULL, // security attributes
PAGE_READWRITE, // protection
high, // high-order DWORD of size
low, // low-order DWORD of size
fileName.c_str()); // name
if (hFileMapping == NULL)
{
dwError = GetLastError();
sprintf_s(szBuf, "[CShareMemoryImage]CreateFileMapping failed,GetLastError = %d\n", dwError);
DebugStringA(szBuf);
CloseHandle(hFile);
LeaveCriticalSection(&m_cs);
return FALSE;
}
else
{
DebugStringA("[CShareMemoryImage]CreateFileMapping success!\n");
LPVOID pViewOfFile = MapViewOfFile( // 映射
hFileMapping, // handle to file-mapping object
FILE_MAP_ALL_ACCESS, // desired access
0,
0,
0); // map all file
if (pViewOfFile == NULL)
{
dwError = GetLastError();
sprintf_s(szBuf, "[CShareMemoryImage]MapViewOfFile failed,GetLastError = %d\n", dwError);
DebugStringA(szBuf);
CloseHandle(hFile);
CloseHandle(hFileMapping);
LeaveCriticalSection(&m_cs);
return FALSE;
}
else
{
sprintf_s(szBuf, "[CShareMemoryImage]MapViewOfFile success,fileName = %s\n", fileName.c_str());
DebugStringA(szBuf);
}
}
LeaveCriticalSection(&m_cs);
return TRUE;
}
{
EnterCriticalSection(&m_cs);
char szBuf[256] = { 0 };
// 获取最小分页大小
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
DWORD dwAllocationGranularity = sysinfo.dwAllocationGranularity;
INT64 dwMemoryFileSize = 1024*1024; //指定内存映射文件大小
// 必须要为dwAllocationGranularity(64K)的整数倍
int left = dwMemoryFileSize % dwAllocationGranularity;
if (left != 0)
{
dwMemoryFileSize += (dwAllocationGranularity - left);
}
DWORD dwError = 0;
// 文件方式创建共享内存
HANDLE hFile = CreateFileA(fileName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
sprintf_s(szBuf, "[CShareMemoryImage]CreateFile failed,GetLastError = %d\n", dwError);
DebugStringA(szBuf);
LeaveCriticalSection(&m_cs);
return FALSE;
}
DebugStringA("[CShareMemoryImage]CreateFile success!\n");
DWORD high = (dwMemoryFileSize >> 32) & 0xFFFFFFFF;// 高32位
DWORD low = dwMemoryFileSize & 0xFFFFFFFF;// 低32位
HANDLE hFileMapping = CreateFileMappingA(
hFile, // system paging file INVALID_HANDLE_VALUE
NULL, // security attributes
PAGE_READWRITE, // protection
high, // high-order DWORD of size
low, // low-order DWORD of size
fileName.c_str()); // name
if (hFileMapping == NULL)
{
dwError = GetLastError();
sprintf_s(szBuf, "[CShareMemoryImage]CreateFileMapping failed,GetLastError = %d\n", dwError);
DebugStringA(szBuf);
CloseHandle(hFile);
LeaveCriticalSection(&m_cs);
return FALSE;
}
else
{
DebugStringA("[CShareMemoryImage]CreateFileMapping success!\n");
LPVOID pViewOfFile = MapViewOfFile( // 映射
hFileMapping, // handle to file-mapping object
FILE_MAP_ALL_ACCESS, // desired access
0,
0,
0); // map all file
if (pViewOfFile == NULL)
{
dwError = GetLastError();
sprintf_s(szBuf, "[CShareMemoryImage]MapViewOfFile failed,GetLastError = %d\n", dwError);
DebugStringA(szBuf);
CloseHandle(hFile);
CloseHandle(hFileMapping);
LeaveCriticalSection(&m_cs);
return FALSE;
}
else
{
sprintf_s(szBuf, "[CShareMemoryImage]MapViewOfFile success,fileName = %s\n", fileName.c_str());
DebugStringA(szBuf);
}
}
LeaveCriticalSection(&m_cs);
return TRUE;
}
相关文章推荐
- 微信自定义菜单中文乱码问题
- 千分位设置
- weka up-sampling & down-sampling
- Factorial Trailing Zeroes
- 浅谈大型web系统架构
- 《数据结构与算法分析c++描述》读书笔记二——二叉树
- CentOS 7: 设置时区和时间
- c#, extract number from string
- Unity开发优化----删除多余的MeshCollider和Animation组件
- 进程与线程的区别(转)
- java基础--继承,抽象,接口
- Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析
- Action的生命周期
- Gitlab的使用
- 在MFC中使用SQLite
- ASP.NET 使用Ajax
- java 线程查看
- ASP.NET 使用Ajax
- C++求两个数的最大值
- Problem 9.Palindrome Number