CreateFileMapping/MapViewOfFile
2013-02-13 17:48
1416 查看
CreateFileMapping/MapViewOfFile
for MapViewOfFile function
If hFile is INVALID_HANDLE_VALUE, the calling process must also specify a size for the file mapping object in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. In this scenario, CreateFileMapping creates a file mapping object of a specified size that is
backed by the system paging file instead of by a file in the file system.
dwFileOffsetLow [in]
A low-order DWORD of the file offset where the view is to begin. The combination of the high and low offsets must specify an offset within the file mapping. They must also match the memory allocation granularity of the system. That is, the offset must be a
multiple of the allocation granularity. To obtain the memory allocation granularity of the system, use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure.
SysInfo.dwAllocationGranularity = 65536;
in sysMapFileSegmentInShmem d:/linux/linuxkernel/WORKING_DIRECTORY/android-omap-20111108-gingerbread/dalvik/libdex/SysUtil.c
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
// adjust = start % SYSTEM_PAGE_SIZE;
adjust = start % SysInfo.dwAllocationGranularity;
for CreateFileMapping function:
After a file mapping object is created, the size of the file must not exceed the size of the file mapping object; if it does, not all of the file contents are available for sharing.
So when CreateFileMapping with a smaller size than the actual file size, the offset larger than CreateFileMapping size.
dwMaximumSizeLow [in]
The low-order DWORD of the maximum size of the file mapping object.
If this parameter and dwMaximumSizeHigh are 0 (zero), the maximum size of the file mapping object is equal to the current size of the file that hFile identifies.
how to read/write mapping file:
http://msdn.microsoft.com/zh-cn/library/aa366801
Reading and Writing From a File View (Windows)
The following example uses the pointer returned by MapViewOfFile to read from the file view:
C++
DWORD dwLength;
__try
{
dwLength = *((LPDWORD) lpMapAddress);
}
__except(GetExceptionCode()==EXCEPTION_IN_PAGE_ERROR ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
// Failed to read from the view.
}
The following example uses the pointer returned by MapViewOfFile to write to the file view:
C++
DWORD dwLength;
__try
{
*((LPDWORD) lpMapAddress) = dwLength;
}
__except (GetExceptionCode() == EXCEPTION_IN_PAGE_ERROR ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
// Failed to write to the view.
}
The FlushViewOfFile function copies the specified number of bytes of the file view to the physical file, without waiting for the cached write operation to occur:
C++
if (!FlushViewOfFile(lpMapAddress, dwBytesToFlush))
{
printf("Could not flush memory to disk (%d).\n", GetLastError());
}
http://www.cnblogs.com/yukaizhao/archive/2011/05/18/MapViewOfFile_CreateFileMapping.html
C++使用内存映射文件入门
//向内存映射视图中写数据
CopyMemory((PVOID)mmfm_base_address, write_chars, write_chars_size);
//读数据
memcpy(read_chars,mmfm_base_address,write_chars_size);
http://msdn.microsoft.com/en-us/library/aa366551(v=vs.85).aspx
Creating Named Shared Memory
CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
Second Process
A second process can access the string written to the shared memory by the first process by calling the OpenFileMapping function specifying the same name for the mapping object as the first process. Then it can use the MapViewOfFile function to obtain a pointer
to the file view, pBuf. The process can display this string as it would any other string. In this example, the message box displayed contains the message "Message from first process" that was written by the first process.
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
http://msdn.microsoft.com/en-us/library/aa366543(v=vs.85).aspx
Creating a File Mapping Using Large Pages
for MapViewOfFile function
If hFile is INVALID_HANDLE_VALUE, the calling process must also specify a size for the file mapping object in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. In this scenario, CreateFileMapping creates a file mapping object of a specified size that is
backed by the system paging file instead of by a file in the file system.
dwFileOffsetLow [in]
A low-order DWORD of the file offset where the view is to begin. The combination of the high and low offsets must specify an offset within the file mapping. They must also match the memory allocation granularity of the system. That is, the offset must be a
multiple of the allocation granularity. To obtain the memory allocation granularity of the system, use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure.
SysInfo.dwAllocationGranularity = 65536;
in sysMapFileSegmentInShmem d:/linux/linuxkernel/WORKING_DIRECTORY/android-omap-20111108-gingerbread/dalvik/libdex/SysUtil.c
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
// adjust = start % SYSTEM_PAGE_SIZE;
adjust = start % SysInfo.dwAllocationGranularity;
for CreateFileMapping function:
After a file mapping object is created, the size of the file must not exceed the size of the file mapping object; if it does, not all of the file contents are available for sharing.
So when CreateFileMapping with a smaller size than the actual file size, the offset larger than CreateFileMapping size.
dwMaximumSizeLow [in]
The low-order DWORD of the maximum size of the file mapping object.
If this parameter and dwMaximumSizeHigh are 0 (zero), the maximum size of the file mapping object is equal to the current size of the file that hFile identifies.
how to read/write mapping file:
http://msdn.microsoft.com/zh-cn/library/aa366801
Reading and Writing From a File View (Windows)
The following example uses the pointer returned by MapViewOfFile to read from the file view:
C++
DWORD dwLength;
__try
{
dwLength = *((LPDWORD) lpMapAddress);
}
__except(GetExceptionCode()==EXCEPTION_IN_PAGE_ERROR ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
// Failed to read from the view.
}
The following example uses the pointer returned by MapViewOfFile to write to the file view:
C++
DWORD dwLength;
__try
{
*((LPDWORD) lpMapAddress) = dwLength;
}
__except (GetExceptionCode() == EXCEPTION_IN_PAGE_ERROR ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
// Failed to write to the view.
}
The FlushViewOfFile function copies the specified number of bytes of the file view to the physical file, without waiting for the cached write operation to occur:
C++
if (!FlushViewOfFile(lpMapAddress, dwBytesToFlush))
{
printf("Could not flush memory to disk (%d).\n", GetLastError());
}
http://www.cnblogs.com/yukaizhao/archive/2011/05/18/MapViewOfFile_CreateFileMapping.html
C++使用内存映射文件入门
//向内存映射视图中写数据
CopyMemory((PVOID)mmfm_base_address, write_chars, write_chars_size);
//读数据
memcpy(read_chars,mmfm_base_address,write_chars_size);
http://msdn.microsoft.com/en-us/library/aa366551(v=vs.85).aspx
Creating Named Shared Memory
CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
Second Process
A second process can access the string written to the shared memory by the first process by calling the OpenFileMapping function specifying the same name for the mapping object as the first process. Then it can use the MapViewOfFile function to obtain a pointer
to the file view, pBuf. The process can display this string as it would any other string. In this example, the message box displayed contains the message "Message from first process" that was written by the first process.
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
http://msdn.microsoft.com/en-us/library/aa366543(v=vs.85).aspx
Creating a File Mapping Using Large Pages
相关文章推荐
- CreateFileMapping 、MapViewOfFile、UnmapViewOfFile函数用法及示例(转载)
- Windows API一日一练(59)CreateFileMapping和MapViewOfFile函数
- CreateFileMapping, MapViewOfFile 实现文件共享
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- CreateFileMapping, MapViewOfFile 实现文件共享
- Windows API一日一练(59)CreateFileMapping和MapViewOfFile函数
- CreateFileMapping 、MapViewOfFile、UnmapViewOfFile函数用法及示例
- 使用文件映射的方式进行共享数据中CreateFileMapping、MapViewOfFile函数参数说明
- CreateFileMapping/OpenFileMapping/MapViewOfFile
- CreateFileMapping, MapViewOfFile 实现文件共享 (.net4.0 built-in)
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- [转]Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- CreateFileMapping、MapViewOfFile
- CreateFileMapping ,MapViewOfFile和OpenFileMapping的用法
- CreateFileMapping/OpenFileMapping/MapViewOfFile
- CreateFileMapping和MapViewOfFile
- (转)CreateFileMapping/OpenFileMapping/MapViewOfFile
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- Windows API学习(一)MapViewOfFile后一定要UnMapViewOfFile